Rails / Formtastic - number (ID) for association instead of select - formtastic

I'm building a form for a class with a belongs_to association, and the parent class is identified by ID and has a ton of records - a select doesn't make sense at all. I would like to just ask for the ID of the parent record instead. I tried just changing the input type:
= semantic_form_for #child do |f|
= f.input :parent, as: :number
But that renders the incorrect name on the input field:
<input name="child[parent]" type="number">...</input>
So I swapped the input name to parent_id:
= semantic_form_for #child do |f|
= f.input :parent_id, as: :number
This renders the input as I want, but it doesn't hook the validations up correctly because they are attached to the parent association rather than parent_id.
class Child
belongs_to :parent
validate do
if parent.name != "valid name"
errors.add :parent, "is invalid"
end
end
end
That validation attaches to parent instead of parent_id so it doesn't render on the form. Attaching to parent_id also doesn't feel right.
What's the correct way to do this?

Related

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

grouping and fields_for

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...

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

ActiveRecord Validation: association saved even if validation failed

Errors are added to error object of record but associations are still saved.
class Parent < ActiveRecord::Base
validate :valid_child?
#validation methods
protected
def valid_child?
#child_names = Hash.new
self.children.each do |curr_child|
if #child_names[curr_child.name].nil?
#child_names[curr_child.name] = curr_child.name
else
errors.add(:base, "child name should be unique for children associated to the parent")
end
end
end
#associations
has_and_belongs_to_many :children, :join_table => 'map__parents__children'
end
#query on rails console
#parent = Parent.find(1)
#parent.children_ids = [1, 2]
#parent.save
The problem is that, for an existing record, #parent.children_ids = [1, 2] will take effect a change in the database before the call to #parent.save.
Try using validates_associated to validate the children rather than rolling your own validation.
To make sure that the children's names are unique within the context of the parent, use validates_uniqueness_of with the :scope option to scope the uniqueness to the parent id. Something like:
class Child < ActiveRecord::Base
belongs_to :parent
validates_uniqueness_of :name, :scope => :parent
end