ruby - How to save nested form data in Rails? -
i have data inside foods , drinks table:
foods table
id | name ------------ 1 | rojak 2 | lemang
drinks table
id | name ------------ 1 | barli 2 | sirap
my model relationship is:
class survey < activerecord::base has_many :questions accepts_nested_attributes_for :questions, allow_destroy: true end class question < activerecord::base belongs_to :survey has_many :answers accepts_nested_attributes_for :answers, allow_destroy: true end class answer < activerecord::base belongs_to :question has_many :foods has_many :drinks accepts_nested_attributes_for :foods, :drinks end class food < activerecord::base belongs_to :answer end class drink < activerecord::base belongs_to :answer end
and _form.html.erb file inside app/views/surveys:
<%= form_for @survey |f| %> <% if @survey.errors.any? %> <div class="error_messages"> <h2><%= pluralize(@survey.errors.count, "error") %> prohibited survey being saved:</h2> <ul> <% @survey.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <br> <%= f.fields_for :questions |f| %> <fieldset> <%= f.label :content, "question" %><br /> <%= f.text_area :content %><br /> <% food.all.each |fd| %> <fieldset> <%= f.fields_for :answers |f| %> <%= f.text_field :name, :value => fd.name %> <%= f.number_field :quantity %> <% end %> <br> </fieldset> <% end %> </fieldset> <br> <% end %> <br> <div class="actions"> <%= f.submit %> </div> <% end %>
what i'm trying is:
- list foods
name
, drinksname
inside foods , drinks fieldset - give each of foods , drinks
quantity
(thisquantity
column inside answers table) - save , update it
but got no luck. when i'm try load edit view (the form), managed list of foods name. but, i've no idea how list of drinks name in own fieldset.
i try make didn't work:
<% if :content == "foods" %> <%= #loads foods %> <% else %> <%= #loads drinks %> <% end %>
and, when try save foods/drinks name , quantity, didn't work too.
demo:
how fix problem?
note:
this update method inside surveys_controller:
def update if @survey.update(survey_params) redirect_to @survey, notice: "successfully updated survey." else render :edit end end ... def survey_params params.require(:survey).permit(:name, questions_attributes: [:id, :_destroy, :content, answers_attributes: [:id, :_destroy, :content, :quantity]]) end
update:
this show.html.erb
<h1><%= @survey.name %></h1> <ul class="questions"> <% @survey.questions.each |question| %> <li> <%= question.content %> <ol class="answers"> <% question.answers.each |answer| %> <li><%= answer.content %> (<%= answer.quantity %>)</li> <% end %> </ol> </li> <br> <% end %> </ul> <%= link_to 'edit', edit_survey_path(@survey) %> | <%= link_to 'back surveys', surveys_path %>
you might seeing issue variable scope. each time create nested form f.fields_for
pass in variable f
, though f
variable used in parent.
try calling different questions , answers, such as:
<%= f.fields_for :questions |fq| %> <fieldset> <%= fq.label :content, "question" %><br /> <%= fq.text_area :content %><br /> <% food.all.each |fd| %> <fieldset> <%= fq.fields_for :answers |fa| %> <%= fa.text_field :name, :value => fd.name %> <%= fa.number_field :quantity %> <% end %> <br> </fieldset> <% end %> </fieldset> <br> <% end %>
Comments
Post a Comment