Why is this render :partial line iterating my collection twice? - ruby-on-rails

Given these relationships:
class Account < ActiveRecord::Base
has_many :employments
has_many :people, :through => :employments
accepts_nested_attributes_for :employments
end
class Employment < ActiveRecord::Base
belongs_to :account
belongs_To :person
end
I'm trying to list the employment records for an account:
<% form_for #account do |f| -%>
<% f.fields_for :employments do |e| -%>
<%= render :partial => 'employment', :collection => #account.employments, :locals => { :f => e } %>
<% end -%>
<% end -%>
I've verified that the employment table in #account contains two records, but I get four copies of the partial because it iterates employments twice:
Employment Load (1.0ms) SELECT * FROM [employments] WHERE ([employments].account_id = 1)
Person Load (1.3ms) SELECT * FROM [people] WHERE ([people].[id] = 2)
Rendered accounts/_employment (17.9ms)
Person Load (1.5ms) SELECT * FROM [people] WHERE ([people].[id] = 1)
Rendered accounts/_employment (5.1ms)
Rendered accounts/_employment (2.2ms)
Rendered accounts/_employment (2.1ms)
Can anybody explain why that would happen?
Here's some additional information:
The _employment.html.erb partial:
<div class="employment">
<span class="name"><%= link_to h(employment.person.name), person_path(employment.person) %></span>
<span class="role"><%=h employment.role %></span>
<span class="commands"><%= remove_child_link "Remove", f %></span>
</div>
remove_child_link is the only place I need to generate a form field at. It creates the _delete field for the record and wires up a remove link that changes the value to '1'. The 'role' property may also be editable, though. The important thing is I don't want all of the fields to be editable.
The accounts_controller actions for this view:
def edit
#account = Account.find(params[:id])
end
def update
#account = Account.find(params[:id])
respond_to do |format|
if #account.update_attributes(params[:account])
flash[:notice] = "#{#account.business_name} was successfully updated."
format.html { redirect_to #account }
else
format.html { render :action => "edit" }
end
end
end
Ben got me going in the right direction. Some runtime inspection reveals that the record is stored in the object variable (which I already knew, but in a different context). So I can rewrite the fields_for clause as:
<% form_for #account do |f| -%>
<% f.fields_for :employments do |e| -%>
<div class="employment">
<span class="name"><%= link_to h(e.object.person.name), person_path(e.object.person) %></span>
<span class="role"><%=h e.object.role %></span>
<span class="commands"><%= remove_child_link "Remove", e %></span>
</div>
<% end -%>
<% end -%>

Its rendering that partial for each field in the employments model - when really you want to do it once for each employment record. That is, remove the iteration over the fields_for:
<% form_for #account do |f| -%>
<%= render :partial => 'employment', :collection => #account.employments %>
<% end -%>

You are correct to use fields_for, but it will render what's inside of it for each employment, so remove the :collection parameter from render :partial. Instead, use nested forms by putting this in your Account model:
accepts_nested_attributes_for :employments
read more about nested forms here: http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

Related

Only One Product Variant added to Cart, and It's always the first one added to my db

I'm setting up a simple online store with no user system - just a session following a cart.
Currently, I am able to select a Product (could be thought of as a product category), and within that product page select a ProductVariant (product_variant_id) belonging to it.
The problem is that when I add different product_variants, only the first one that is saved to the db is ever added to the cart. I can select the product_variants in the dropdown, but again only the first one added to the db in my record is added to the cart as an order_item.
My relevant Models:
Product
has_many :product_variants, dependent: :destroy
has_many :order_items, :through => :product_variants
ProductVariant
belongs_to :product
has_many :order_items, dependent: :destroy
OrderItem
belongs_to :order, optional: true
belongs_to :cart, optional: true
belongs_to :product_variant
belongs_to :product
Cart
has_many :order_items
And here is my product show page in show.html.erb with the option to select a product_variant
<%= link_to products_path do %>
<h4>Back to store gallery</h4>
<% end %>
<section class="flexbox">
<div class="flex">
<%= image_tag #product.image_1.show.url %>
</div>
<div class="flex">
<h2><%= #product.title %></h2>
<div class="product-description">
<h5>Description:</h5>
<p><%= #product.description %></p>
</div>
</div>
</section>
<%= simple_form_for [#product, #product_variant, #order_item] do |f| %>
<%= f.input :quantity %>
<%= f.button :submit, "Add to cart" %>
<% end %>
Product selection: <br>
<select name="product[product_variant_id]">
<% #product.product_variants.each do |product_variant| %>
<option value="<%= product_variant.id %>"><%= product_variant.item %>/<%= product_variant.size %>/<%= product_variant.color %>/<%= number_to_currency product_variant.price_in_dollars %></option>
<% end %>
</select>
Lastly, here is my order_items controller
class OrderItemsController < ApplicationController
def create
#product = Product.friendly.find(params[:product_id])
# find the product_variant
#product_variant = ProductVariant.find(params[:product_variant_id])
# quantity? - comes from the form data
#quantity = form_params[:quantity]
#current_cart.order_items.create(product: #product, product_variant: #product_variant, quantity: #quantity)
flash[:success] = "Thanks for adding to your cart"
redirect_to product_path(#product)
end
def update
#product = Product.friendly.find(params[:product_id])
#product_variant = ProductVariant.find(params[:product_variant_id])
#order_item = OrderItem.find(params[:id])
#order_item.update(form_params)
flash[:success] = "Thanks for updating your cart"
redirect_to cart_path
end
def destroy
#product = Product.friendly.find(params[:product_id])
#product_variant = ProductVariant.find(params[:product_variant_id])
#order_item = OrderItem.find(params[:id])
#order_item.delete
flash[:success] = "Product removed from cart"
redirect_to cart_path
end
def form_params
params.require(:order_item).permit(:quantity)
end
end
Thanks for any insight into what might be going wrong here, and please don't hesitate if you need me to provide any more relevant code, would be happy to.
Aaron

RoR edit value of join table from nested form

I am a noob on Ror. Been looking for my problem answers for 3 days now, I have been looking for the answers but can’t find one with my specific problem. ( I even found it hard to write the right title)
So I have been trying to build a nested form in RoR. I have a simple order form that enable users to specify the quantity they ordered in that form. The form will only store the value into the database if the quantity text field is not empty.
The Order form is simply look like this:
I am storing the quantity data into the join table between order and inventory which has many to many relationship through Inventory_orders table. now in the Inventory_orders table instead of only having orders_id and inventories_id , I also add the quantity column.
now I have been able to get the form working with the code below:
Controller:
def new
#order = Order.new
#customer = Customer.all
#inventories_list = Inventory.all
#inventory_order = #order.inventory_orders.build
end
def create
#order = Order.new(order_params)
#inventories_list = Inventory.all #controller can call any model
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def order_params
params.require(:order).permit(:customer_id, :order_ids => [],:inventory_orders_attributes => [:id, :quantity, :inventory_id ])
end
View:
<%= form_for(#order) do |f| %>
<div id = “Main_Container">
*** Some Code ***
<table id = "inventory_table">
<tr>
<td class = "prodCodeTitle"><h3>Prod Code</h3></td>
<td class = "prodResult"><h3>Quantity</h3></td>
<td class = "prodResult"><h3>Size</h3></td>
<td class = "prodResult"><h3>Price</h3></td>
</tr>
//Here display all the inventories list
<% #inventories_list.each do |t| %>
<tr>
<td class ="prodResult"><%= link_to t.pName, inventory_path(t), :remote => true %></td>
<td class = “prodResult">
//nested form for the join table
<%= f.fields_for :inventory_orders do |qty| %>
<%= qty.hidden_field :inventory_id , value: t.id %>
<%= qty.number_field :quantity %>
<%end%>
</td>
<td class = "prodResult"><%= t.pMeter %></td>
<td class = "prodResult"><%= t.pSellPrice %></td>
</tr>
<% end %>
*** Some Code***
<% end %>
Model:
class Order < ActiveRecord::Base
belongs_to :customer
has_many :inventory_orders
has_many :inventories, through: :inventory_orders
validates :customer_id, :presence => true
accepts_nested_attributes_for :inventory_orders, :reject_if => lambda { |a| a[:quantity].blank?}
end
class InventoryOrder < ActiveRecord::Base
belongs_to :inventory
belongs_to :order
validates :quantity, :presence => true
end
Now when creating new Orders form , the application works and store the data that I want in the inventory_orders table.
The problem occurs when I try to edit the form. When trying to click on edit button I get this output in my View file:
for example this is what I Input into the form:
when I try to edit the form this is what I get:
this is my controller for edit:
def edit
#order = Order.find(params[:id])
#customer = Customer.all
#inventories_list = Inventory.all
end
I have been looking at the psql database schema by manual do sql query as follow:
select * from inventory_orders where inventory_orders.order_id = 63;
and get this as result:
now it seems that the fields_for Inventory_orders get the number of rows returned , but I don't get why all the quantity also get displayed 4 times for each product. Also how can I ensure that when I try to edit quantity for product “aaa” it will only display one number_field with what users has input before.
Sorry for the long post,otherwise I am not sure how to clearly convey my meaning.
EDITED
this to show my Inventory Model:
Class Inventory < ActiveRecord::Base
has_many :inventory_orders
has_many :orders, through: :inventory_orders
end
You need to use the following:
//Here display all the inventories list
<% #inventories_list.each do |t| %>
<%= link_to t.pName, inventory_path(t), :remote => true %>
<%= f.fields_for :inventory_orders, t do |qty| %>
<%= qty.hidden_field :inventory_id , value: t.id %>
<%= qty.number_field :quantity %>
<% end %>
<%= t.pMeter %>
<%= t.pSellPrice %
<% end %>
The issue is that since f.fields_for populates a form based on the built associated objects, if you're passing 4 fully constructed objects through the edit action, fields_for is going to populate all of them each time.
What you need is to use the instance of the associated data.
I think your code could be improved a lot:
#app/controllers/orders_controller.rb
class OrdersController < ApplicationController
def new
#order = Order.new
#inventory = Inventory.all
#inventory_order = #order.inventory_orders.build
end
def edit
#order = Order.find params[:id]
#inventory = Inventory.all
end
end
#app/views/orders/new.html.erb
<%= form_for #order do |f| %>
<% #inventory.each do |inventory| %>
<%= f.fields_for :inventory_orders, item do |item| %>
<%= item.text_field :quantity %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
#app/views/orders/edit.html.erb
<%= form_for #order do |f| %>
<%= f.fields_for #order.inventory_orders do |item| %>
<%= item.text_field :quantity %>
<% end %>
<%= f.submit %>
<% end %>

Rails select tag using belongs_to association

I have the following models:
Product:
class Product < ActiveRecord::Base
belongs_to :user
...
end
User
class User < ActiveRecord::Base
has_many :products
end
In my products view I need a drop down list of users, and when a user is selected, it should be associated with that specific product.
This is my view:
<%= form_tag (update_user_products_path) do %>
<%#products.each do |product| %>
<div class = "prod">
<img src='<%= product.cover_img %>' class='product_image_prods'></img>
<div class= "title"><small><b><%= link_to truncate(product.title, :length =>30), recommendations_path(:product_id => product.id, :rating_set_id => params[:rating_set_id]), :target => '_blank' %></b></small></div>
<br/>
<div><em>Current Rating: <%= product.rating %> </em></div>
<%= hidden_field_tag :product_id, product.id %>
<%= select_tag "user_id", options_for_select(User.all.collect {|u| [ u.name, u.id ] })%>
</div>
<% end %>
<%= submit_tag %>
<% end %>
I am confused as to if I should use form_tag and updated the Product.user_id, or should use nested_attributes for the user model in my product from?
UPDATE:
Products Controller Action:
def update_user
#product = Product.find(params["product_id"])
#product.update_attribute(:user_id, params[:user_id])
redirect_to :back, :flash => { :notice => "Updated users" }
end
Also updated my view. How do I update all product records with the correct user_id. Currently, when I submit my from, it updates just 1 product record.
All you have to do is figure which user was associated with which product.
For example (hooking the tag with product.id)
<%= form_tag(update_user_path, :id => "update_user_form") do %>
<%#products.each do |product| %>
<%= select_tag "user_id:#{product.id}", options_for_select(User.all.collect {|u| [ u.name, u.id ] })%>
<% end %>
<% end %>
When you submit you will get the the key with the product id and the selected user's id.
For example lets say product.id = 1 and user.id = 2
params will include
key => user_id:1 value=> 2
so taking the key and splitting it on : will give you the product_id then getting the value of that hash entry will give you the selected user.id for that product.

Rails has_many checkboxes with extra attribtues

We have the following code working for a complex rails form with checkboxes. I'm not really happy with the solution we have in place and I was wondering if anyone knows of a more proper way to do this in rails. All the code below is working I just want to know if there is a cleaner approach.
In my Admins controller I want to remove the need to call the following code on each update.
#user.admin.school_admin_roles.destroy_all
params[:roles].each do |school_role|
ids = school_role.split('_')
#user.admin.school_admin_roles.find_or_create_by_school_id_and_school_role_id(ids[0], ids[1])
end if !params[:roles].nil?
So I basically want to be able to call #user.update_attributes(params[:user]) and have rails take care of creating the needed relationships for me. I have that working with AccountRole in the form below. I want to know if there is a way to do the same thing with SchoolRole given I have an extra variable school_id in the join table.
We have the following form for editing a user and assigning roles
Screenshot of form ->
http://i.stack.imgur.com/PJwbf.png
I have the following form where an admin can edit other users and assign account based roles and school based roles via checkboxes. The account based roles were easy to implement. The school based rules are a bit complicated since the join table school_admin_roles has school_id, user_id, role_id fields. We had to implement the school roles part of the form in a rather hackish way. We have the form implemented like this - notice how we hacked together school.id.to_s+'_'+role.id.to_s into the same checkbox on school roles.
In the Admins controller's update function we manually destroy all school_admin roles on each update then loop through the school roles params do a split on the ids on '-' then manually re-create each school based role. I really hate the way we've had to go about this. Could anyone shed some light on a cleaner more rails centric approach to solving this scenario?
The form -
<%= form_for #user, :url => {:controller => 'admins', :action => 'update'} do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.fields_for :admin do |uf| %>
<div class="field">
<%= uf.label :first_name %>
<%= uf.text_field :first_name %>
</div>
<label>Admin Permissions</label>
#account level permissions works fine
<%= hidden_field_tag "#{uf.object_name}[account_role_ids][]" %>
<% AccountRole.find(:all).each do |role| %>
<div class="account_role">
<%= check_box_tag "#{uf.object_name}[account_role_ids][]", role.id, #user.admin.account_roles.include?(role)%>
<%= role.name %>
</div>
<% end %>
#school level permissions a bit of a hack
<%= hidden_field_tag "#{uf.object_name}[school_role_ids][]" %>
<% SchoolRole.find(:all).each_with_index do |role, index| %>
<div class="school_role">
<%= check_box_tag "#{uf.object_name}[school_role_ids][]",role.id, #user.admin.school_roles.include?(role) %>
<%= role.name %>
<span class="advanced_box admin_permissions" <% if #user.admin.school_roles.include?(role) %>style="display:inline"<% end %>>
<div class="content" id="perm_<%= index %>">
<h4><%= role.name %></h4>
<% uf.object.account.schools.each do |school|%>
<div>
<%= check_box_tag "roles[]", school.id.to_s+'_'+role.id.to_s, role.school_admin_roles.where(:admin_id => uf.object.id).collect(&:school_id).include?(school.id)%>
<%= school.name %>
</div>
<% end %>
<%= link_to 'Done', '#', :class => "done" %>
</div>
Advanced
</span>
</div>
<% end %>
</div>
<% end %>
The controller
class AdminsController < ApplicationController
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
# TODO find a way to refactor this
#user.admin.school_admin_roles.destroy_all
params[:roles].each do |school_role|
ids = school_role.split('_')
#user.admin.school_admin_roles.find_or_create_by_school_id_and_school_role_id(ids[0], ids[1])
end if !params[:roles].nil?
#
flash[:notice] = "Successfully updated Admin."
redirect_to admins_path
else
render "edit"
end
end
end
Given the following models
class User < ActiveRecord::Base
has_one :parent
has_one :admin
has_many :scool_admin_roles
has_many :account_admin_roles
end
class AccountAdminRole < ActiveRecord::Base
before_save :set_account_id
belongs_to :admin
belongs_to :account_role
end
class SchoolAdminRole < ActiveRecord::Base
belongs_to :admin
belongs_to :school_role
belongs_to :school
end
class SchoolRole < ActiveRecord::Base
has_many :school_admin_roles
end
class AccountRole < ActiveRecord::Base
has_many :account_admin_role
end
When I face code that I know smells bad, usually it leads me to the design.
In this case, the problem is the database table design.
You are hacking the value passed from a checkbox with a delimiter because the "join" table does more than just join. I believe that the relationship to school belongs_to the SchoolRole and not the SchoolAdminRole. Changing this will create a pattern much like your AccountRole.
Correcting the model design, might be a bit painful now, but it is much cleaner and will be maintainable in the future. You will thank yourself later.
We refactored the code above as follows
In the model we added accepts_nested_attributes_for :school_admin_roles, :reject_if => proc { |attr| attr['school_role_id'].blank? }
and added school_admin_roles_attributes to attr_accessible
class Admin < ActiveRecord::Base
belongs_to :account
belongs_to :user
has_many :school_admin_roles
has_many :school_roles, :through => :school_admin_roles
has_many :account_admin_roles
has_many :account_roles, :through => :account_admin_roles
accepts_nested_attributes_for :account
accepts_nested_attributes_for :school_admin_roles, :reject_if => proc { |attr| attr['school_role_id'].blank? }
attr_accessible :account_role_ids, :email, :first_name, :last_name, :account_id, :user_id, :account_attributes, :school_admin_roles_attributes
default_scope where(:deleted => false)
end
We then built the form as follows
<% index2 = 0 %>
<% SchoolRole.find(:all).each_with_index do |role, index| %>
<div class="school_role">
<%= check_box_tag "school_roles[]",role.id, #user.admin.school_roles.include?(role) %>
<%= role.name %>
<span class="advanced_box admin_permissions" <% if #user.admin.school_roles.include?(role) %>style="display:inline"<% end %>>
div class="content" id="perm_<%= index %>">
<h4><%= role.name %></h4>
<% uf.object.account.schools.each do |school|%>
<div>
<%= check_box_tag "#{uf.object_name}[school_admin_roles_attributes][#{index2}][school_role_id]", role.id, role.school_admin_roles.where(:admin_id => uf.object.id).collect(&:school_id).include?(school.id)%>
<%= school.name %>
<%= hidden_field_tag "#{uf.object_name}[school_admin_roles_attributes][#{index2}][school_id]", school.id %>
</div>
<% index2 += 1 %>
<% end %>
<%= link_to 'Done', '#', :class => "done" %>
</div>
Advanced
</span>
</div>
<% end %>
</div>
<% end %>
Which then enabled us to refactor the controller without splitting the ids but we still have to call destroy all each time which I can live with.
def update
#user = User.find(params[:id])
#user.admin.school_admin_roles.destroy_all
if #user.update_attributes(params[:user])
flash[:notice] = "Successfully updated Admin."
redirect_to admins_path
else
render "edit"
end
end

Cannot get nested form with a has_one association to work

I have these models:
class User < ActiveRecord::Base
has_one :city
accepts_nested_attributes_for :city
end
class City < ActiveRecord::Base
belongs_to :user
end
This controller action:
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.xml { render :xml => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.xml { render :xml => #user.errors, :status => :unprocessable_entity }
end
end
end
and this view:
<%= form_for :user,:url => users_path,:method => :post do |f| %>
<%= f.fields_for :city do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I am trying to allow the user to select a city from the list of already added cities. I am trying to present him a select. The select part it works, but the generated html code for it, looks like this:
<select name="user[city][id]" id="user_city_id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
Notice that it's name doesn't have attribute anywhere. So, when I try to save it, I get this error:
City(#37815120) expected, got ActiveSupport::HashWithIndifferentAccess(#32969916)
How can I fix this?
EDIT: there is some progress, I tried to change the fields_for to this:
<%= f.fields_for :city_attributes do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
and now, the html seems to generate correctly. But I get this error now:
Couldn't find City with ID=1 for User with ID=
I have no idea what to do next.
EDIT2: overriding the city_attributes= method seems to work:
def city_attributes=(attribs)
self.city = City.find(attribs[:id])
end
I don't know if it's the way to go, but it seems good.
Have a look at this question that seems similar to yours :
Rails 3: How does "accepts_nested_attributes_for" work?
Actually, since the Cities already exsit, I think there is no need for nested forms here.
Try Replacing
<%= f.fields_for :city_attributes do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
With
<%= f.collection_select :city, City.all,:id,:name %>
Updated afters comments
Could you change your relationship with (and update database scheme accordingly)
class User < ActiveRecord::Base
belongs_to :city
end
class City < ActiveRecord::Base
has_many :users
end
And then try using:
<%= f.collection_select :city_id, City.all,:id,:name %>
You could also do a
<%= f.collection_select :city_id, City.all, :id, :name %>
in your view and then add virtual attributes to your User model:
class User < ActiveRecord::Base
...
def city_id(c_id)
update_attribute(:city, City.find(c_id))
end
def city_id
city.id
end
end
This might not be very clean, since the associated City model is "saved" whenever assigning an ID to some_user.city_id. However, this solution keeps your controller and view nice and clean.
Note: you might also want to account for a blank ID being passed in to the setter method.
Try this
<%= f.select(:city_id, City.all.collect {|p| [ p.name, p.id ] }) %>

Resources