2016-08-21 2 views
0

私はコンサート登録アプリになっているシンプルなレールアプリを開発しています。基本的には、人からの情報を取得してから、DBに保存して、一意のIDを持つチケットを生成することです。問題は、フォームがチケットのフィールドを表示していないことです。これらのフィールドが表示される唯一の時間は、どこかにエラーがある場合です。隠しフィールドのネストされたフォームの不明な理由ruby on rails 5

@ticket = @participant.tickets.build 

が何であるかは目を追加します。 enter image description here

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+\-.][email protected][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 

答えて

1

あなたのcreateアクションの行でありますeチケット。したがって、レールのネストされたフォームやsimple_formを使用する場合でも、必要なレコード数で子関係をインスタンス化する必要があります。だから、それをループして表示します。 newアクションをレンダリングするときは、1枚のチケットをしたいのであれば、単純に呼び出す:新しいアクションで

@participant.tickets.build 

を。

+0

問題を解決していただきありがとうございます。 今、私は 'チケット 'が' db'に保存されていることに何も気づいていません。私はイドとは別にすべてを「nil」とする。どんな手掛かり? –

+0

参加者は保存していますが、チケットは保存されていませんか? 'attributes ['kind']。blank?'が含まれているreject_ifブロックを置いていますが、kind属性への参照はどこにもありません。それはあなたが使っているものですか? – agmcleod

+0

私はそれがインターネットからの悪いコピー貼り付けだと思う​​。参加者は保存中であり、チケットも保存されていますが、作成および更新の時間を節約するだけですが、他の属性は保存されません。 –

関連する問題