2014-11-11 3 views
0

У меня есть глупая проблема, но я не могу понять, в чем причина. Вложенные атрибуты модели не спасаемRails 4: вложенные атрибуты не сохраняются

poll.rb

class Poll < ActiveRecord::Base 

    has_many :poll_options 
    accepts_nested_attributes_for :poll_options 

    validates :name, presence: true 

end 

poll_option.rb

class PollOption < ActiveRecord::Base 

    belongs_to :poll 

    validates :name, presence: true 

end 

polls_controller

 def new 
     @poll = Poll.new 
     @poll.poll_options.build 
     end 

     def create 
     @poll = Poll.create(poll_params) 
     if @poll.save 
      redirect_to poll_path(@poll) 
     else 
      render 'new' 
     end 
     end 

     private 

     def poll_params 
     params.require(:poll).permit(:name, :description, poll_options_attributes: [:id, :name]) 
     end 

вид

= simple_form_for @poll do |poll| 
    = poll.input :name 
    = poll.input :description 
    = simple_fields_for :poll_options do |option| 
    = option.input :name 
    = poll.button :submit 

Что я пропустил? Спасибо

ответ

2

Это должно быть poll.simple_fields_for, а не только simple_fields_for.

+0

обязательно, спасибо за помощь –

+0

Нет проблем. Пожалуйста, примите ответ, когда сможете, спасибо! –