【问题标题】:Unknown reason for hidden fields nested forms ruby on rails 5在rails 5上隐藏字段嵌套表单的未知原因
【发布时间】:2016-12-28 05:31:54
【问题描述】:

我正在开发一个简单的 Rails 应用程序,它应该是一个音乐会注册应用程序。基本上应该做的是从一个人那里获取信息,然后将它们保存在DB 中,同时生成一个具有唯一 ID 的票证。问题是表单没有显示工单的字段。这些字段显示的唯一时间是某处出现错误时。

participants_controller
    def new
        @participant = Participant.new
        @ticket = @participant.tickets.build
      end

      def create
        @participant = Participant.new(participant_params)
        @ticket = @participant.tickets.build
        1.times{@ticket}
        if @participant.save
          flash[:success] = "An Email as been sent to #{@participant.email}"
          redirect_to root_path
        else
          render :new
        end
      end

      private
      def participant_params
        params
            .require(:participant)
            .permit(:first_name,:last_name,:phone,:email,:gender,:email_confirmation,:participant_id,
                    :tickets_attributes => [:id,:vip,:quantity])
      end

然后是表格

= simple_form_for @participant do |f|
  = f.error_notification
  = f.input :first_name
  = f.input :last_name
  = f.input :phone
  = f.input :email
  = f.input :email_confirmation
  = f.input :gender, collection: ['M','F'], as: :radio_buttons
  = f.simple_fields_for :tickets do |ticket|
    = ticket.input :vip , label: 'Want a vip ticket? ',collection: ['No','Yes'] , include_blank: false
    = ticket.input :quantity , label: 'How many tickets?' , include_blank: false, collection: 1..100
  = f.button :submit , 'Get Tickets'

然后是模型

ticket model
class Ticket < ApplicationRecord
  belongs_to :participant
  # validates :quantity, presence: true
  validates :participant , presence: true
end




participant model
class Participant < ApplicationRecord

  validates :first_name,:last_name, presence: true
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
            format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false },
            confirmation: true
  validates :email_confirmation, presence: true
  VALID_PHONE_NUMBER = /\d/
  validates :phone, presence: { message: "Won't give it to NSA promise" }, format:{with: VALID_PHONE_NUMBER},
            length: {is: 11}
  has_many :tickets , inverse_of: :participant
  accepts_nested_attributes_for :tickets,
                                reject_if: lambda {|attributes| attributes['kind'].blank?}
end

【问题讨论】:

    标签: ruby-on-rails forms validation haml


    【解决方案1】:

    创建操作中的行:

    @ticket = @participant.tickets.build
    

    是什么添加门票。因此,使用 rails 嵌套表单,或者甚至使用 simple_form 时,您需要使用所需的记录数来实例化子关系。所以它将遍历它们并显示它们。因此,如果您在 new 操作呈现时想要一张票,只需调用:

    @participant.tickets.build
    

    在新动作中。

    【讨论】:

    • 感谢您解决了一个问题。现在我意识到Ticket 中的任何内容都没有保存在db 中。除了id,我得到了所有nil。有什么线索吗?
    • 参与者正在保存,但票不是?我看到您放置了一个包含:attributes['kind'].blank? 的 reject_if 块,但我在任何地方都没有看到对 kind 属性的引用。那是你正在使用的东西吗?
    • 我认为这是来自互联网的糟糕复制粘贴。参与者正在保存,票证也正在保存,但它只保存了创建和更新的时间,而没有保存其他属性。
    • 检查 rails 服务器日志以查看 strong_parameters 是否阻止了任何通过。可能需要调整您的 permit() 调用
    • 我不知道发生了什么,但在重新启动服务器后它工作了,非常感谢帮助伙伴。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    相关资源
    最近更新 更多