grouping and fields_for - ruby-on-rails-3

I'm trying to create a form which allows me to submit new records for an association where the association inputs are grouped.
class Product < AR::Base
has_many :properties
accepts_nested_attributes_for :properties
end
Note that in the controller a series of properties are built for the product, so #product.properties.empty? # => false.
The below fields_for gives me the correct inputs with names such as product[properties_attributes][0][value].
= form.fields_for :properties do |pform|
= pform.input :value
But as soon as I try and group the association it no longer generates inputs with the correct names:
- #product.properties.group_by(&:group_name).each do |group_name, properties|
%h3= group_name
= form.fields_for properties do |pform|
= pform.input :value
This create inputs which the name attribute like product[product_property][value] when in fact it should be product[property_attributes][0][value] as per the first example.
The Rails documentation suggests you can do this:
= form.fields_for :properties_attributes, properties do |pform|
But this gives an error "undefined method value for Array".

You need to set up like this:
- #product.properties.group_by(&:group_name).each do |group_name, properties|
%h3= group_name
= form.fields_for :properties, properties do |pform|
= pform.text_field :value
It should work fine, since you have accepts_nested_attributes_for :properties rails know that it's supposed to create fields for properties_attributes. Moreover you may want to add
attr_accessible :properties_attributes if you are using one of newest Rails and if you didn't add it to your model yet ;)
Also, if you want to make some decision base on single property you may use the following form as well:
- #product.properties.group_by(&:group_name).each do |group_name, properties|
%h3= group_name
- properties.each do |property|
= form.fields_for :properties, property do |pform|
= pform.text_field :value
Both of them are nicely described here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for under One-To-Many section

I don't know of a clean 'Rails' solution to this sort of thing, but I often handle these types of situations more manually. eg - Loop through the groups and conditionally show only those properties that are in a particular group.
class Product < AR::Base
has_many :properties
accepts_nested_attributes_for :properties
def group_names
properties.map(&:group_name).uniq.sort
end
end
In the view
- for group_name in product.group_names
= form.fields_for :properties do |pform|
%h3= group_name
- if pform.object.group_name.eql?(group_name)
= pform.input :value
There may be a little overhead with this, getting the list of properties from product repeatedly. You may be able to modify your has_many :properties association to retrieve the properties in group_name order - then you could add the %h3 when it's a new group.
- previous_group_name = nil
= form.fields_for :properties do |pform|
- if pform.object.group_name != previous_group_name
%h3= pform.object.group_name
= pform.input :value
- previous_group_name = pform.object.group_name
Some ideas for you...

Related

Using nested attributes to easily select associations in a form

I am trying to create a nested attribute form to create a model which is primarily an association "connector" between two other models. In my case, the models represent books, awards, and the "connector" model book_awards. When I am editing a book, I want to be able to quickly select which awards it has won.
I've been using
http://railscasts.com/episodes/196-nested-model-form-part-1
to help me get started, but I'm afraid I'm pretty much stuck.
Another SO question which seems similar is
accepts_nested_attributes_for with find_or_create? Unfortunately, it's also not quite what I'm doing and I haven't been able to adapt it.
My models look like this. Each model has additional attributes and validations etc, but I've removed them for clarity.
class Book < ActiveRecord::Base
has_many :book_awards
accepts_nested_attributes_for :book_awards, :allow_destroy => true
end
class Award < ActiveRecord::Base
has_many :book_awards
end
class BookAward < ActiveRecord::Base
belongs_to :book, :award
end
In my book controller methods for edit and new, and the failure cases for create and update I have a line #awards = Award.all.
In my view, I would like to see a list of all awards with check boxes next to them. When I submit, I would like to either update, create, or destroy a book_award model. If the check box is selected, I would like to update an existing model or create a new one if it doesn't exist. If the check box isn't selected, then I would like to destroy an existing model or do nothing if the award never existed. I have a partial for book_awards. I'm not sure if the check box selector should be in this partial or not.
I think my check box will be my hook to :_destroy but with its polarity reversed. I think something like this will basically do it:
= f.check_box :_destroy, {}, 0, 1
Currently, I have this in my partial but I'm not sure where it really belongs.
Next comes my view which currently doesn't work, but maybe it will help demonstrate what I'm trying to do. I loop through the awards and use a fields_for to set nested attributes for anything that already exists. It's horribly ugly, but I think it somewhat works. However, I don't really know how to get started with the else case.
= f.label :awards
- #awards.each do |a|
- if f.object.awards && f.object.awards.include?(a)
= f.fields_for :book_awards, f.object.book_award.select{|bas| bas.award == a } do |ba|
= render 'book_awards', :f => ba, :a => a
- else
= fields_for :book_awards do |ba|
= render 'book_awards', :f => ba, :a => a
I would prefer the awards to be listed in the same order each time (my #awards assignment in the controller will probably specify the order) as opposed to listing the existing awards first or last.
I hate to answer my own question, but I finally figured out something which works. The first thing I needed to do was to update the "new" case based on the crazy object which was included in the railscast. Next, I needed to manually set the :child_index. Finally, I needed to manually set the :_destroy check box appropriately.
.field
= f.label :awards
- #awards.each_with_index do |a,i|
- if exists = (f.object.awards && f.object.awards.include?(a))
- new_ba = f.object.book_awards.select{|s| s.award == a}
- else
- new_ba = f.object.class.reflect_on_association(:book_awards).klass.new
= f.fields_for :book_awards, new_ba, :child_index => i do |ba|
= render 'book_awards', :f => ba, :a => a, :existing => exists
My partial looks like this:
.field
= f.check_box :_destroy, {:checked => existing}, 0, 1
= f.label a.name
= f.hidden_field :award_id, :value => a.id
= f.label :year
= f.number_field :year
It's not horribly pretty, but it seems to do exactly what I wanted.

Maintaining Many to Many relation with checkboxes (e.g. checkboxes for each category a post can be in)

We have a table with Categories containing unique categories. And our posts should link to any of these categories. I tried to add checkboxes for selecting the categories in the following manner:
Add the checkboxes containing the categories in a field called "post[categoriesss][]", containing as value the :id of the category.
Update the new and update method to take these these ids and collect the associated Categories with them, and then add each category to the categories attribute through the << operator as stated in the documentation.
We added the code to new and update to be sure not to have to do this in multiple controllers.
However we get the following error:
undefined property or relationship '<<' on Category
What are we doing wrong here? Is there another method to achieve our goal? Our code is below.
class Post
include DataMapper::Resource
property :id, Serial
has n, :categories, :through => Resource
def self.new(hash={})
if hash.key? :categoriesss
hash[:categoriesss].each do |category|
categories << Category.get(category)
end
hash.delete! :categoriesss
end
super hash
end
end
class Category
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :rekenservices, :through => Resource
end
<% Category.all.each do |category| %>
<label>
<input type="checkbox" name="post[categoriesss][]" value="<%= category.id %>" />
<%= category.name %>
</label>
<% end %>
UPDATE:
I now changed the names for the checkbox back to categories and updated the new and update methods to this:
def self.new(hash={})
if hash.key? :categories
hash[:categories].map! {|id| Category.get id}
end
super hash
end
def update(hash={})
if hash.key? :categories
hash[:categories].map! {|id| Category.get id}
end
super hash
end
Adding categories the first time works. When updating checking an extra category works great as well. Unchecking a category however yields an error message:
columns category_id, post_id are not unique

Rails: Retrieving polymorphic data from nested model

I'm trying to retrieve data for all items from a box, a box can have compartments and I'd like to get all compartment info at the box level. items are made polymorphic as boxes won't necessarily have compartments.
MODEL
class Box < ActiveRecord::Base
has_many :compartments
has_many :items, :as => :itemable
end
In my Controller I can get results back with:
#box = Box.find(params[:id])
#itemable = #box.compartments.first
#itemable = #box.compartments.last
VIEW
<% #items.each do |item| %>
<%= item.name %>
<% end %>
but if I then try
#itemable = #box.compartments
OR
#itemable = #box.compartments.find(:all)
I get the error
undefined method `items' for #<ActiveRecord::Array>
OR
undefined method `items' for #<ActiveRecord::Relation>
Can anyone help with getting results back from all compartments?
So in compartments you have
belongs_to :box
has_many :items, :as => :itemable
Is this the case? #box.compartments should return an array of compartments right? It sounds like items is somehow getting called on #box.compartments somehow

Rails - how to display value through associations in form_for?

This is my view:
= form_for(#user) do |f|
= f.autocomplete_field #user.city.name, autocomplete_city_name_users_path
On the second line I am trying to display the association, but I am getting
undefined method `London' for #<User:0x00000129bb3030>
The associations:
User belongs_to :city
City has_one :user
The displayed result in the error message (London) is right, but why I am gettng that error message?
The argument to f.autocomplete_field should be the name of a method. The form builder will send this method to #user to get the correct value. Since the value you're interested in is not in user but in an object owned by user, you have a few options:
Add city_name and city_name= methods to your User class:
# app/models/user.rb
def city_name
city && city.name
end
def city_name=(name)
city.name = name # You'll want to make sure the user has a city first
end
If you don't know how to make sure you have a city, you could create one lazily by changing your city_name= method to this:
def city_name=(name)
build_city unless city
city.name = name
end
Then your form would look like this:
= form_for(#user) do |f|
= f.autocomplete_field :city_name, autocomplete_city_name_users_path
Or you could treat this as a nested object. Add this to User:
accepts_nested_attributes_for :city
And use fields_for in your form:
= form_for(#user) do |f|
= f.fields_for :city do |city_f|
= city_f.autocomplete_field :name, autocomplete_city_name_users_path

Using ActiveRecord::Association methods where associated "child" model uses STI

I've got a super-class (model) Measurement and two sub-classes: WeightMeasurement and LengthMeasurement.
I've then got a Person class which as many WeightMeasurements and LengthMeasurements.
The issue is when creating a new measurement for a Person, I want to use a shared controller that will handle both weight and length measurements.
However, the way that I would typically build up a Person's measurements would be access them bia the parent (Person). Like person.weight_measurement.build. The problem is that I don't know what to put here... person..build ?
# Base-model, includes "type" column in database.
class Measurement < ActiveRecord::Base
belongs_to :person
end
# model subclass
class WeightMeasurement < Measurement
end
# model subclass
class LengthMeasurement < Measurement
end
class Parent < ActiveRecord::Base
has_many :weight_measurements, :dependent => :destroy
has_many :length_measurements, :dependent => :destroy
end
# Single controller for "Measurements"
class MeasurementsController < ApplicationController
...
def new
person = Person.find(params[:person_id])
#
normally would do this, but because I am using STI,
# I don't know that it is a Person's "weight" measurement we are creating
#
# #measurement = #person.weight_measurements.build
#
...
end
...
end
What I normally do, is to create a hidden field in my form, which contains the type I am trying to create.
<%= hidden_field_tag :type, "weight_measurement" %>
You could also have it as a visible form option (say a radio button or select - instead of the hidden field above)
In your controller, you can do the following then:
if ["length_measurement", "weight_measurement"].include?(params[:type])
#measurement = "Measurement::#{params[:type].classify}".constantize.new(:person => #person)
end