0
我正在開發一個簡單的軌道應用程序,應該是一個音樂會註冊應用程序。基本上應該做的是從一個人那裏獲取信息,然後將它們保存在DB
中也會生成一張帶有唯一ID的票證。問題是表單沒有顯示票據的字段。這些字段顯示的唯一時間是某處出現錯誤時。 隱藏字段的未知原因在軌道上嵌套窗體ruby 5
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
謝謝你解決了一個問題。 現在我意識到沒有任何關於'Ticket'的信息被保存在'db'中。除了id之外,我得到所有'nil'。任何線索? –
參與者正在保存,但票證不是?我看到你把一個reject_if塊包含:'attributes ['kind']。blank?',但我在任何地方都看不到對某種kind屬性的引用。這是你正在使用的東西嗎? – agmcleod
我認爲這是來自互聯網的糟糕的複製粘貼。參與者正在保存並且票據正在保存,但它僅保存創建和更新的次數,但其他屬性未保存。 –