Rails 4 fields_for not displaying field -
i have 2 models create 1 form. tried following railscasts tutorial, can't nested fields display on form. how can make these nested fields appear?
models
class poll < activerecord::base has_many :poll_answers, :dependent => :destroy accepts_nested_attributes_for :poll_answers, allow_destroy: true end class pollanswer < activerecord::base belongs_to :poll end
controller
class pollscontroller < applicationcontroller def new @poll = poll.new 2.times { @poll.poll_answers.build } end private def poll_params params.require(:poll).permit([ :question, poll_answers_attributes: [:answer] ]) end end
view
<%= form_for(@poll) |f| %> <div class="field"> <%= f.label :question %><br> <%= f.text_field :question %> </div> <% f.fields_for :poll_answers |pa| %> <p>hello <%= pa.text_field :answer %> </p> <% end %> <%= debug @poll.poll_answers %> <div class="actions"> <%= f.submit %> </div> <% end %>
you missed =
<%= f.fields_for :poll_answers |pa| %> <p>hello <%= pa.text_field :answer %> </p> <% end %>
Comments
Post a Comment