rails - printing text using iteration - ruby-on-rails

I am trying tp print a non updateable value in my page.
So far it displays
<%= f.fields_for :ingredients do |builder| %>
<fieldset>
<%= builder.text_area :name %>
</fieldset>
<% end %>
which works and I need it to display a non updatable - plain text. So I tried to do
Option 1
<%= f.fields_for :ingredients do |builder| %>
<fieldset>
<%= builder.label :name, "Ingredients" %>
</fieldset>
<% end %>
But no success. this does work:
option 2
<% #recipe.ingredients.each do |ingredient| %>
<td><%= ingredient.name %></td>
<% end %>
What is the main difference between option 1 and 2 ? I prefer using option 1 with the <li> tag. what am I doing wrong?

Label is used to create a label for a field, and not meant to show its value. Option 2 is exactly how you would display the contents of the name field. Option 1 style of formatting is to show the literal 'Ingredients' instead of 'name'. Ususally, that would be followed by <%= builder.text_area :name %>.
Another possible inconsistency: #recipe.ingredients is an array, so there is no field called ingredients.name. As a test, try ingredients[0].name.
So in option 1, try:
<% ingredient = #recipe.ingredients[0] %>
<%= f.fields_for :ingredient do |builder| %>
<fieldset>
<%= builder.label :name, "Ingredients" %>
</fieldset>
<% end %>
If that printed the label 'Ingredients', then you could print the list of ingredients with:
<td>Ingredients:</td>
<% #recipe.ingredients.each do |ingredient| %>
<td><%= ingredient.name %></td>
<% end %>

Related

Showing check box values in edit page

I'm using the following collection_select in the new and edit pages to let users select contacts and associate them to a group:
<%= f.label :contacts %>
<div><span class="ul">
<% current_user.contacts.all.each do |contact| %>
<%= check_box_tag "contacts[]", contact.id %>
<%= f.label contact.name %>
<% end %></div>
</span>
I would like to show the already selected contacts checked in the edit page of the group. Is there another parameter that I can use along with the check_box_tag?
You can just pass in a true/false after the value
<%= f.label :contacts %>
<div>
<span class="ul">
<% current_user.contacts.all.each do |contact| %>
<% checked_logic = some logic for true/false %>
<%= check_box_tag "contacts[]", contact.id, checked_logic %>
<%= f.label contact.name %>
<% end %>
</span>
</div>
<% end %>

Rails form_for check_box

I'm trying to create a basic survey app. On my take survey page I'm looping through and displaying each answer option as a radio button or checkbox as a form_for to create a user's choice. The choices are working great for the questions that are single choice (or radio buttons), but they aren't saving for multi select questions. I'm pretty sure this has to do with the form I have for the checkbox.
It seems like I should do
<%= f.check_box :answer_id, answer.id %> <%= answer.title %> <br>
similar to how I'm creating the radio button but that throws an error
undefined method `merge' for 14:Fixnum
Here's my code that displays:
<h3>Questions:</h3>
<ul><% #survey.questions.each do |question| %>
<li><p><%= question.title %></p></li>
<% choice = question.choices.build %>
<% if question.single_response == true %>
<%= form_for [question, choice] do |f| %>
<% question.answers.each do |answer| %>
<%= f.radio_button :answer_id, answer.id %> <%= answer.title %><br>
<% end %>
<%= f.hidden_field :survey_id, value: #survey.id %>
<%= f.submit %>
<% end %>
<br />
<% else %>
<%= form_for [question, choice] do |f| %>
<% question.answers.each do |answer| %>
<%= f.check_box :answer_id %> <%= answer.title %> <br>
<%= f.hidden_field :survey_id, value: #survey.id %>
<% end %>
<%= f.submit %>
<% end %>
<br />
<% end %>
<% end %>
</ul>
Any idea what I need to do to get it to save the answer_id to the choice so that it actually creates the choice?
Thanks!
This question is a few years old but I think it deserves a better answer. Since you are using form_for (a model backed form), then you probably want to use the form_for check_box method that you originally tried to use. In your case, it would look like this:
<%= f.check_box :choice, { :multiple => true }, answer.id, false %>
Here is the doc on this.
For checkboxes, you actually want to return an array as the parameter. There is a little funny syntax to this because we don't actually want to use the form builder methods. It should look something like this (adapt to your specific model names and methods)
<%= check_box_tag 'choice[answer_ids][]', answer.id %>
Using this syntax should tell Rails to compile all of the checked checkbox values into an array.
This Railscast goes over the topic.

Rails Nested Attributes Doesn't Insert ID Correctly

I'm attempting to edit a model's nested attributes, much as outline here, replicated here:
<%= form_for #person do |person_form| %>
<%= person_form.text_field :name %>
<% for address in #person.addresses %>
<%= person_form.fields_for address, :index => address do |address_form|%>
<%= address_form.text_field :city %>
<% end %>
<% end %>
<% end %>
In my code, I have the following:
<%= form_for(#meal) do |f| %>
<!-- some other stuff that's irrelevant... -->
<% for subitem in #meal.meal_line_items %>
<!-- # Edit 2: I need to display information here about the subitem
Which I can't find a way to pass it to the partial, or work in
this manner for existing items
-->
<%= subitem.food.name %>
<%= subitem.food.calories %>
<%= f.fields_for subitem, :index => subitem do |line_item_form| %>
<%= line_item_form.label :servings %><br/>
<%= line_item_form.text_field :servings %><br/>
<%= line_item_form.label :food_id %><br/>
<%= line_item_form.text_field :food_id %><br/>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
This works great, except, when I look at the HTML, it's creating the inputs that look like the following, failing to input the correct id and instead placing the memory representation(?) of the model. As a result, an update fails:
<input type="text" value="2" size="30" name="meal[meal_line_item][#<MealLineItem:0x00000005c5d618>][servings]" id="meal_meal_line_item_#<MealLineItem:0x00000005c5d618>_servings">
EDIT:
The reason I'm attempting to do it in this method is that I need to gather some information on associations for existing meal_line_items. For example, in the area where I took out code, I have some code to the effect of:
<%= subitem.food.name %>
<%= subitem.food.calories %>
Getting this information won't work if I am using a form builder with partials, at least, not in my trials.
Edit 2:*
See the edit in the code. Here's my MealLineItem
class MealLineItem < ActiveRecord::Base
# Associations ---------------------
belongs_to :food
belongs_to :meal
end
And meal accepts_nested_attributes for the model. As you can see it belongs to both food and meal model. For the existing meal_line_item I need to do something like:
meal_line_item.food.name
Is there f. missing from <%= fields_for?
--edit
Have you tried:
<%= f.fields_for 'meal[meal_line_item][]', subitem do |line_item_form| %>
--edit
Docs say that it should work without loop too:
<%= form_for(#meal) do |f| %>
<!-- some other stuff that's irrelevant... -->
<%= f.fields_for :meal_line_items do |line_item_form| %>
<%= line_item_form.label :servings %><br/>
<%= line_item_form.text_field :servings %><br/>
<%= line_item_form.label :food_id %><br/>
<%= line_item_form.text_field :food_id %><br/>
<% end %>
<%= f.submit %>
<% end %>
Have to test this but maybe this approach?
_form
<%= form.fields_for :meal_line_items do |meal_line_item_form| %>
<% #meal.meal_line_items.each do |meal_line_item| %>
<%= render :partial => "meal_line_items/meal_line_item", :locals => { :meal_line_item_form => meal_line_item_form, :meal_line_item => meal_line_item } %>
<% end %>
<% end %>
meal_line_items/_meal_line_item.erb
<%= meal_line_item_form.label :servings %><br/>
<%= meal_line_item_form.text_field :servings %><br/>
<%= meal_line_item_form.label :food_id %><br/>
<%= meal_line_item_form.text_field :food_id %><br/>
EDIT
here's a link to an example for setting the formbuilder iterator directly (Rails 2.3.8 though). The associations between Outbreak -> Incidents -> Location should be similiar to the ones for Meal -> Meal_line_items -> Food.
AJAX update of accepts_nested_attributes_for partials
After searching high and low, I found the error. Although I was modifying the partial and was receiving a NameError it's because I was calling the partial from a helper method - exactly the same problem as stated in the following question:
rails fields_for render partial with multiple locals producing undefined variable

rails: fields_for and collection

i would like to use an additional collection for a fields_for. this collection should hold all the possibilities to be used in fields_for.
Lets say I have a person with tasks that will happen regularly each week on the same day. In the person form, i should have an entry for each day, even if there are not yet any saved tasks. I tried:
<% form_for(#person) do |f| %>
...
<% f.fields_for :tasks, #weekdays do |task_fields| %>
<%= weekday.name %>:
<%= project_fields.text_field :name %>
<% end %>
<% end %>
now there should be for each weekday a text field to enter the name of the task of that day. for example weekday.name = "monday" and task.name = "drinking coffee", task.weekday_id = 1
You are not iterating through the week_days. You should do like this:
<% #weekdays.each_with_index do |weekday, i| %>
<% f.fields_for :tasks do |task_fields| %>
<%= weekday.name %>:
<%= task_fields.text_field :name %>
<%= task_fields.hidden_field :weekday_id, :value => (i + 1) %>
<% end %>
<% end %>
If you have a table 'weekdays', then hidden_field value should be weekday.id
Edit: July 30
I think I completely messed up this answer. Let me try to improve it.
<% f.fields_for :tasks, #weekdays do |task_fields| %>
<%= weekday = task_fields.object %>
<%= weekday.name %>:
<%= task_fields.text_field :name %>
<%= task_fields.hidden_field :weekday_id, :value => weekday.id %>
<% end %>

A different label for each iteration

in the following code, I want to assign a different label to the text field for each iteration.
<%= f.field_for :skills do |s| %>
<li>
<label>Skills</label>
<%= s.text_field :name %>
</li>
<% end %>
How can I do that?
Here is my controller code, where I create three different skill objects:
def edit
3.times{resource.skills.build}
render_with_scope :edit
end
You could do something like this:
<% counter = 0 %>
<%= f.fields_for :skills do |s| %>
<li>
<%= s.label :name, "Skill #{counter}" %>
<%= s.text_field :name %>
<% counter = counter + 1 %>
</li>
<% end %>
It is preferred to use s.label :name, since that will make sure when you click the label, the text-box will get the focus. But the value of the label can be overruled, as i did here.
I am not quite sure what else you could mean with changing the label for each item, so if you could make that clearer.
Hope this helps.
Just scope the label to s:
<%= f.field_for :skills do |s| %>
<li>
<%= s.label :name, 'Skills' %>
<%= s.text_field :name %>
</li>
<% end %>
I think that should do it.

Resources