0

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

1 Answer 1

2

It should be poll.simple_fields_for, not just simple_fields_for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.