I know I'm missing something, but I can't figure out what.
It's quite nested. I have a quiz which has_many questions which has_many answers. Answers belongs_to Questions which belongs_to a Quiz.
What I have so far:
params.require(:quiz).permit(:name, questions_attributes: [:content, :quiz_id, :explanation, :passage, answers_attributes: [:content, :question_id, :correct_answer]])
My form:
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>
<ul>
<% #quiz.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |question_attribute| %>
<div class = 'inner-c'>
<p>
<%= question_attribute.label :content, "Question" %> <span><b><%= question_attribute.index + 1 %></b></span> <br/>
<%= question_attribute.text_area :content, :cols => 100, :rows => 4 %>
</p>
<p>
<%= question_attribute.label :explanation, "Answer Explanation" %> <br/>
<%= question_attribute.text_area :explanation, :cols => 100, :rows => 6 %>
</p>
<%= question_attribute.label :_destroy, "Remove Question"%>
<%= question_attribute.check_box :_destroy %><br/>
<%= question_attribute.label :passage, "Reference Passage" %> <br/>
<%= question_attribute.text_area :passage, :rows => 3, :class => 'passage-input' %>
<%#= question_attribute.label :question_explanation, "Question Explanation" %>
<%#= question_attribute.text_area :question_explanation, :rows => 10 %>
</p>
<%= question_attribute.fields_for :answers do |answer_attribute| %>
<p>
<%= answer_attribute.label :content, "Answer" %>
<%= answer_attribute.text_field :content %>
<%= answer_attribute.label :correct_answer, "Check to indicate correct answer", :class => 'inline' %>
<%= answer_attribute.check_box :correct_answer, :class => 'inline'%>
</p>
<% end %>
</div> <!-- inner-c -->
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Related
I need to display only four options to questions. At present I am repeating the code four times. I am sure that there is a better way to do it. Can you please tell me how?
form
<%= form_for #question do |form| %>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<%= form.fields_for :options, question.options.build do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= a.check_box :is_answer, checked: true %>
</div>
<% end %>
<%= form.fields_for :options, question.options.build do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= a.check_box :is_answer, checked: true %>
</div>
<% end %>
<%= form.fields_for :options, question.options.build do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= a.check_box :is_answer, checked: true %>
</div>
<% end %>
<%= form.fields_for :options, question.options.build do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= a.check_box :is_answer, checked: true %>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Here I am repeating the block for :options four times. How can I do it in a single block?
In your controller your could do
def create
#question = Question.new
4.times { #question.options.build }
end
And then in your view
<%= form_for #question do |form| %>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<%= form.fields_for :options, question.options.build do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= a.check_box :is_answer, checked: true %>
</div>
<% end %>
<% end %>
That should build 4 of the nested fields for you so you don't have to repeat the code that many times :D
I'm building a quiz app, to create a quiz I do this:
def new
#quiz = Quiz.new
50.times do
question = #quiz.questions.build
5.times { question.answers.build }
end
end
enter code here
Which uses nested parameters. The problem is, sometimes the quiz doesn't save in order (such as when I create a new quiz OR I update an existing quiz). If I create a quiz in particular order, say Question 1, Question 2, Question 3, I want the questions in that quiz to remain in the same order after I update or create it. Right now, if I update a quiz the order gets jumbled up if I want to show the quiz using something like quiz.questions.each do |question|
I wasn't aware that ruby databases don't enforce order, so how do I make sure that if I create a quiz, the order in which I enter the questions (top to bottom) will always be the order that the questions are presented in?
If you are interested, this is the form I use to create the quiz:
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>
<ul>
<% #quiz.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class = 'field'>
<%= f.label :difficulty, "Difficulty of Quiz, 1 to 3 with 3 being most difficult" %>
<%= f.text_field :difficulty %>
</div>
<div class="field">
<%= f.label :for_unsubscribed, "Check to have this quiz be visible to logged in but unsubscribed users" %>
<%= f.check_box :for_unsubscribed %>
</div>
<%= f.fields_for :questions do |question_attribute| %>
<div class = 'inner-c'>
<p>
<%= question_attribute.label :content, "Question" %> <span><b><%= question_attribute.index + 1 %></b></span> <br/>
<%= question_attribute.text_area :content, :cols => 100, :rows => 4 %>
</p>
<p>
<%= question_attribute.label :explanation, "Answer Explanation" %> <br/>
<%= question_attribute.text_area :explanation, :cols => 100, :rows => 6 %>
</p>
<%= question_attribute.label :_destroy, "Remove Question"%>
<%= question_attribute.check_box :_destroy %><br/>
<%= question_attribute.label :passage, "Reference Passage" %> <br/>
<%= question_attribute.text_area :passage, :rows => 3, :class => 'passage-input' %>
<%#= question_attribute.label :question_explanation, "Question Explanation" %>
<%#= question_attribute.text_area :question_explanation, :rows => 10 %>
</p>
<%= question_attribute.fields_for :answers do |answer_attribute| %>
<p>
<%= answer_attribute.label :content, "Answer" %>
<%= answer_attribute.text_field :content %>
<%= answer_attribute.label :correct_answer, "Check to indicate correct answer", :class => 'inline' %>
<%= answer_attribute.check_box :correct_answer, :class => 'inline'%>
</p>
<% end %>
</div> <!-- inner-c -->
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This happens particularly often when I try and update a model.
I have a quiz app, and I add a quiz using this form:
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>
<ul>
<% #quiz.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class = 'field'>
<%= f.label :difficulty, "Difficulty of Quiz, 1 to 3 with 3 being most difficult" %>
<%= f.text_field :difficulty %>
</div>
<div class="field">
<%= f.label :for_unsubscribed, "Check to have this quiz be visible to logged in but unsubscribed users" %>
<%= f.check_box :for_unsubscribed %>
</div>
<%= f.fields_for :questions do |question_attribute| %>
<div class = 'inner-c'>
<p>
<%= question_attribute.label :content, "Question" %> <span><b><%= question_attribute.index + 1 %></b></span> <br/>
<%= question_attribute.text_area :content, :cols => 100, :rows => 4 %>
</p>
<p>
<%= question_attribute.label :explanation, "Answer Explanation" %> <br/>
<%= question_attribute.text_area :explanation, :cols => 100, :rows => 6 %>
</p>
<%= question_attribute.label :_destroy, "Remove Question"%>
<%= question_attribute.check_box :_destroy %><br/>
<%= question_attribute.label :passage, "Reference Passage" %> <br/>
<%= question_attribute.text_area :passage, :rows => 3, :class => 'passage-input' %>
<%#= question_attribute.label :question_explanation, "Question Explanation" %>
<%#= question_attribute.text_area :question_explanation, :rows => 10 %>
</p>
<%= question_attribute.fields_for :answers do |answer_attribute| %>
<p>
<%= answer_attribute.label :content, "Answer" %>
<%= answer_attribute.text_field :content %>
<%= answer_attribute.label :correct_answer, "Check to indicate correct answer", :class => 'inline' %>
<%= answer_attribute.check_box :correct_answer, :class => 'inline'%>
</p>
<% end %>
</div> <!-- inner-c -->
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
A new form would be generated by the controller like so:
def new
#quiz = Quiz.new
50.times do
question = #quiz.questions.build
5.times { question.answers.build }
end
end
Taking advantage of using nested models, where a Quiz has_many Questions which has_many Answers.
The problem is -- when I create a quiz (with 50 questions), then I try and update the quiz afterwards, to fix a mistake for example, The questions move out of order. Question 43 and Question 1 might switch places. I absolutely need the questions to stay in the same order after I update them but I can't figure out how to make this happen. Any ideas?
Generally in RDBMS order is not specified by default, unless you explicitly state it. Therefore if you don't specify the order you can get results in any order an that is an expected behaviour.
But according to your description question 43 swapped with question 1 after an update, so I can think you are ordering them by an updated_at timestamp. Try to order explicitly order questions by id – id is [normally] a PrimaryKey and it never changes, so your questions will stay in order.
How can I rewrite the code in duels/_form so I can put the code from dueler_fields directly in there?
duels/_form
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler| %>
<%= render 'dueler_fields', :f => dueler %>
<% end %>
<%= link_to_add_association f, :duelers do %>
<span class="glyphicon glyphicon-plus"></span> Dueler
<% end %>
<% end %>
_dueler_fields
# I want to place these two lines of code in the _form
<%= f.number_field :user_id, placeholder: "Enter User ID" %>
<%= f.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
duels_controller
def new
#duel = Duel.new
respond_with(#duel)
end
duels has_many duelers.
change f to dueler and you should be good to go. or:
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler_form| %>
<%= dueler_form.number_field :user_id, placeholder: "Enter User ID" %>
<%= dueler_form.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
<% end %>
<% end %>
I currently have a nested model User has_many Sales_Orders has_many Items.
I can create the Sales_Order with nested Items properly but when I try to 'edit' the Sales_Order, the Sales_Order information is visible but the Items are not shown. Any idea why these wouldn't show?
[sales_orders_controller.rb]
...
def edit
#sales_order = SalesOrder.find(params[:id])
end
[edit.html.erb]
<% provide(:title, "Edit SO") %>
<h1>Edit Sales Order</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#sales_order) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="span3">
<%= f.label :so, "SO#:" %>
<%= f.text_field :so %>
<%= f.label :customer, "Customer:" %>
<%= f.text_field :customer %>
<%= f.label :enter_date, "Date Entered:" %>
<%= f.text_field :enter_date, value: date_formatter(#sales_order.enter_date) %>
<%= f.label :request_date, "Request Date:"%>
<%= f.text_field :request_date, value: date_formatter(#sales_order.request_date) %>
<%= f.label :comments, "CS Comments:" %>
<%= f.text_area :comments %>
</div>
<div class="span3">
<% f.fields_for #sales_order.items do |builder| %>
<%= render 'item_fields', f: builder %>
<% end %>
</div>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
[_item_fields.erb]
<fieldset>
<%= f.label :item_code, "Item Code:" %>
<%= f.text_field :item_code %>
<%= f.label :qty_in_kg, "Qty (kg):" %>
<%= f.text_field :qty_in_kg %>
<%= f.label :qc_comments, "Comments:" %>
<%= f.text_field :qc_comments %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
<% f.fields_for #sales_order.items do |builder| %>
should be
<%= f.fields_for #sales_order.items do |builder| %>
Without the = you're building the output in the loop but never rendering/printing the return value (the generated HTML output).
Use <%= f.fields_for #sales_order.items do |builder| %>
Notice the = sign. This implies, "evaluate and embed"