Nested Forms: How to get a "this"? - ruby-on-rails

I use nested forms and would like to get some kind of "this" in it:
The View looks like this:
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<% f.fields_for :assignments do |builder| %>
<%= builder.label :count %>
<%= builder.text_field :count %>
<%= #a.fetch(this.id) %>
<% end %>
The #a is an array with the caption of the assignment. So how can I access the current assignment? Or would this code only display a form for one assignment?
Thanks for your help!

Via #object applied to your builder:
builder.object.id

Related

Is there a way to use form_for without the enclosing <form> tag?

I want to use
<%= form_for(#modelname) do |f| %>
<%= f.label :foo %>
<%= f.text_field :foo %>
<% end %>
And get only the inner HTML, without the enclosing tag. Is it possible?
I believe that what you are looking for is fields_for.
<%= fields_for #model do |f| %>
<%= f.label :foo %>
<%= f.text_field :foo %>
<% end %>

rails form_for nested data doesnt show up

New to rails and getting confused on how to handle this. I am using rails 4. I am very stuck here and will try to talk through this problem.
I have a listings page which I am trying to add tags too. In my view (listings/new.html.erb) I have the following:
<h1> POST A NEW LISTING </h>
<% if current_user.nil? %>
<h2>You must be logged in to view this page </h2>
<% else %>
<%= form_for [#user, #listing] do |f| %>
<%= f.label :title, 'Title' %> <br />
<%= f.text_field :title %>
<%= f.label :general_info, 'General Information' %> <br />
<%= f.text_area :general_info %>
<%= f.label :included, 'Included' %> <br />
<%= f.text_field :included %>
<%= f.label :length, 'Length' %> <br />
<%= f.text_field :length %>
<%= f.label :price, 'Price' %> <br />
<%= f.text_field :price %>
<% fields_for #tagging do |u| %>
<%= u.label :tag, 'Tag' %> <br />
<%= u.text_field :tag %>
<% end %>
<%= f.submit "submit" %>
<% end %>
<% end %>
The form works correctly for putting in the listing, but the tagging form options do not even appear to allow for content.
my listings_conroller #new looks like this:
def new
if (!current_user.nil?)
#user = User.find(current_user.id)
#listing = #user.listings.build
#tagging = #listing.taggings.build
end
end
I want to be able to create a new listing with this form that also populates the database for the tags and I am very unsure on how to do this. I hope this is enough information, but if needed I have all the code here: https://bitbucket.org/r-s/ath/src . Very stuck on this any help would be appreciated.
Change it to:
<%= f.fields_for #tagging do |u| %>
Note the =.
You forgot to use builder:
<%= f.fields_for #tagging do |u| %>

Associated model is not saving data when page is refreshed

Rails 3.1 RC4
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :picture %><br />
<%= f.text_field :picture %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
I'm not sure I agree with your approach. Why don't you do something like this:
In models/user.rb:
accepts_nested_attributes_for :profile
In controllers/users_controller.rb:
def new
#user = User.new
#user.build_profile
end
In views/users/_form.html.erb:
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :profile do |pf| %>
<%= pf.text_field :some_profile_field %>
<% end -%>
<%- end -%>
This isn't copied or tested, but it should work. On saving your user, the profile fields are sent along and validated with the user fields and are re-rendered when rendering the form again after a save error. This way you will keep full control over your form and its contents with minimal effort.

Rails loop through data in a form field

I would like to loop through data in my database inside my form. What I would like to do with the data is put it in labels and textboxes. How could I do this in rails? Would I just use a .each block to loop through it inside the form? The reason I have it in my database is because my client would like to be able to add the form field data himself.
For example here is something I would like to do:
<%= form_for :order do |f| %>
#fields.each do |field|
<%= f.label field.name %>
<%= f.text_field field.name %>
<% end %>
<%= f.submit %>
<% end %>
What the best way to accomplish something like this?
Please don't answer with a railscast :)
Thanks in advance
Yes, that will work, though you missed an end script tag on line two:
<%= form_for :order do |f| %>
<% #fields.each do |field| %>
<%= f.label field.name %>
<%= f.text_field field.name %>
<% end %>
<%= f.submit %>
<% end %>
If you need something more complex than just a label/text field pair - then you can use a partial-template and use the collection keyword:
<!-- in 'order.html.erb' -->
<%= form_for :order do |f| %>
<!-- note: each 'field' is auto-populated from the collection/partial-name, but you need to pass the form in as a local -->
<%= render :partial => 'field', :collection => #fields, :locals => {:f => f} %>
<%= f.submit %>
<% end %>
and
<!-- in '_field.html.erb' -->
<%= f.label field.name %>
<%= f.text_field field.name %>
<!-- and whatever else you want to do... -->
more on partial rendering here: http://api.rubyonrails.org/classes/ActionView/Partials.html

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

Resources