1

I have this data inside my foods and 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 this is my _form.html.erb file inside app/views/surveys:

<%= form_for @survey do |f| %>
  <% if @survey.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
      <ul>
      <% @survey.errors.full_messages.each do |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 do |f| %>
    <fieldset>
    <%= f.label :content, "Question" %><br />
    <%= f.text_area :content %><br />

    <% Food.all.each do |fd| %>
    <fieldset>
      <%= f.fields_for :answers do |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 to do is:

  1. List all foods name and drinks name inside foods and drinks fieldset
  2. Give each of the foods and drinks a quantity (this quantity column is inside answers table)
  3. Save and update it

But I got no luck. When I'm try to load the edit view (the form), I managed to list all of the foods name. But, I've no idea how to list all of the drinks name in its own fieldset.

I try to make it like this but it didn't work:

<% if :content == "Foods" %>
  <%= #loads foods %>
<% else %>
  <%= #loads drinks %>
<% end %>

And, when I try to save the foods/drinks name and its quantity, it didn't work too.

Demo:

enter image description here

How to fix this problem?

Note:

This is 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 is my show.html.erb

<h1><%= @survey.name %></h1>

<ul class="questions">
<% @survey.questions.each do |question| %>
  <li>
    <%= question.content %>
    <ol class="answers">
      <% question.answers.each do |answer| %>
        <li><%= answer.content %> (<%= answer.quantity %>)</li>
      <% end %>
    </ol>
  </li> <br>
<% end %>
</ul>

<%= link_to 'Edit', edit_survey_path(@survey) %> |
<%= link_to 'Back to Surveys', surveys_path %>

1 Answer 1

2

You might be seeing an issue with variable scope. Each time you create a nested form with f.fields_for you pass in the variable f, even though f is the variable used in the parent.

Try calling it something different with questions and answers, such as:

<%= f.fields_for :questions do |fq| %>
  <fieldset>
    <%= fq.label :content, "Question" %><br />
    <%= fq.text_area :content %><br />

    <% Food.all.each do |fd| %>
    <fieldset>
      <%= fq.fields_for :answers do |fa| %>
      <%= fa.text_field :name, :value => fd.name %>
      <%= fa.number_field :quantity %>
    <% end %> <br>
  </fieldset>
  <% end %>

</fieldset>
<br>
<% end %>
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.