Reputation: 1974
I have a stupid problem, but I can't figured out what the cause of it. Nested attributes of model not saving
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
view
= 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
What did I miss? Thanks
Upvotes: 0
Views: 116
Reputation: 14402
It should be poll.simple_fields_for
, not just simple_fields_for
.
Upvotes: 2