I've setup some models in the table inheritance fashion and everything seems to be all fine and dandy. But, when I use a collection select field to select values from one of the models it saves it but it saves the ID of the data and not the actual value of the data. So when I try to display the value on the show view it just shows the corresponding ID and not the actual value.
Here is my setup. I'm using formtastic as a side note.
View
<%= show_field "County", #company.county %>
Form
<%= f.input :county, :label => 'County', :as => :select, :collection => County.find(:all) %>
Base Model
class Tag < ActiveRecord::Base
before_create :set_type
before_update :set_type
attr_accessible :type, :name, :category
belongs_to :company
def set_type
self.type = self.category
end
end
Inherited Model
class County < Tag
end
The tag/county is supposed to store the id not the value. Within your view you can then reference the name field of the county, or whichever field you wish to show
<%= show_field "County", #company.county.name %>
Related
I have an f.select input on my Rails app that comes from this helper method.
def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method)
collection.map do |group|
option_tags = options_from_collection_for_select(
group.send(group_method), option_key_method, option_value_method)
content_tag(:optgroup, option_tags, :label => group.send(group_label_method))
end.join.html_safe
end
The select in the view is shown below.
<%= f.select(:type_id, option_groups_from_collection_for_select(#categories, :types, :category, :id, :name)) %>
When saving the Post, the correct type_id is getting saved, but when I go and edit the post, the select doesn't show the currently selected item like it's supposed to. I'm assuming something is wrong in my code.
Here is my category model
has_many :posts
has_many :types, :order => "name"
and here is my type model
belongs_to :category
You have to provide a 5th argument which is the selected key. Try the code below:
<%= f.select(:type_id, option_groups_from_collection_for_select(#categories, :types, :category, :id, :name, f.object.type_id)) %>
f.object.type_id returns the type_id attribute of the object passed in the form if it has one. Otherwise, it would be nil and will not have anything selected.
I had the same problem and I found that I had simply misspelled the object's name in my controller file. It did not save because the matching object could not be found.
I'm building a daily deal Rails app to learn RoR.
I am facing a problem for the past few hours : i can't get a model's attribute of an other associated model on active admin. Let me show you exactly the problem :
I have two models: Brand (i.e the brand of the deal) and Deal. A deal belongs to a Brand but a Brand can have many Deals.
models/deal.rb is like this:
class Deal < ActiveRecord::Base
belongs_to :brand
and we have models/brand.rb:
class Brand < ActiveRecord::Base
has_many :deals
attr_accessible :name
And i did the t.belongs_to in my migrations so this is ok.
In Active Admin's Deals' create form , i type, as admin, which brand the deal is associated with:
admin/game.rb
ActiveAdmin.register Deal do
# -- Form -----------------------------------------------------------
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :brand_id, :label => "Select a brand:", :as => :select, :collection => Brand.all
end
it works great, and i can create Deals with a certain brand.
but I CAN'T manage to display the NAME of the Brand in my list of Deals:
ActiveAdmin.register Deal do
index do
selectable_column
# id_column
column :title
column :deal_amount
column :brand do |deal|
link_to deal.brand.name
end
...doesn't work.
How can I do that ?
I tried everything but i basically don't know how to fetch the name of a Brand given it matches the brand_id in the Deal's table.
Any help appreciated.
show do |f|
panel "Subject" do
attributes_table_for f, :name, :description, :is_visible
end
panel "Pages in List View" do
table_for(f.pages) do |page|
column :name
column :permalink
column :is_visible
end
end
panel "Pages in View " do
div_for(f.pages) do |page|
panel page.name do
attributes_table_for page, :name, :description, :is_visible
end
end
end
end
end
You can do nested relations in same style as parent model
A couple things seem missing:
class Deal < ActiveRecord::Base
belongs_to :brands, foreign_key: :brand_id, class_name: 'Brand'
end
This is assuming that you mean partner to be a Brand and your schema uses brand_id for that relationship.
In your form, you can simply use:
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :partner, label: 'Select a brand:'
end
end
Your link_to call won't actually link to a url the way you have it.
column :brand do |deal|
link_to deal.partner.name, admin_brand_path(deal.partner)
# or simpler
auto_link deal.partner
end
I would highly recommend trying to be consistent in your naming, as it will make things a lot less confusing and will require less code to make things work. i.e.
class Deal < ActiveRecord::Base
belongs_to :brand
end
f.input :brand, label: 'Select a brand:'
auto_link deal.brand
And your DB column can still be named brand_id.
I'm trying to figure out how to construct a collection_select to include two relationships. Here are my models:
class Country < ActiveRecord::Base
has_many :companies, :dependent => :destroy
end
class Company < ActiveRecord::Base
belongs_to :country
has_many :departments, :dependent => :destroy
end
class Department < ActiveRecord::Base
belongs_to :company
end
When I create a new company I use the following to show a select box based on the relationship.
<%= collection_select(:company, :country_id, Countries.all, :id, :name, :prompt => 'Please select country') %>
But for the departments I'd like to have a select which let's the user select it's company from a select which also includes the companies country, formatted in the following way:
Company 1 - Country 1
Company 2 - Country 1
If i use the following I will only get a list of all the companies which I'd like to be able to see from the list which country they are from.
<%= collection_select(:device, :cabinet_id, Cabinet.all, :id, :name, :prompt => 'Please select cabinet') %>
Is there a way for rails to pull the information for the country into a select and append the entry with it's parent country?
I hope I've worded this question correctly! Sorry if it isn't clear.
Even if #jvnil solution works, I think you should avoid putting this logic in your view.
Instead, you could create an instance method in your Company model and use it in your select.
In your model :
class Company< ActiveRecord::Base
def name_for_select
name + " - " + country.name
end
end
And in your view :
<%= collection_select(:department, :company_id, Company.all, :id, :name_for_select %>
Use
UPDATE: move logic code to model
# company.rb
def company_with_country
"#{name} - #{country.name}" # this is better than using `string + string` since it creates only 1 string
end
# view
collection_select :department, :company_id, Company.includes(:country).all, :id, :company_with_country
UPDATE: faster version because it only uses needed columns
# controller
#companies = Company.joins(:country)
.select('companies.id, companies.name, countries.name AS country_name')
.map { |c| ["#{c.name} - #{c.country_name}", c.id] }`
# view
select :department, :company_id, #companies
I have two models:
Project.rb
class Project < ActiveRecord::Base
belongs_to :customer
end
and Customer.rb
class Customer < ActiveRecord::Base
has_many :projects
end
Inside the _form.html.erb I have:
<p>
<label>Select Customer</label>
<%= f.collection_select :customer_id, Customer.all, :id, :name, :include_blank => true %>
</p>
Which should Collect the Customers from the Customer model and display all the customers, finally it should assign the value to the customer_id which is in projects table.
Rite now the everything is passing when i check the log. When I select the first customer with value=1, it passes customer_id = "1" in my log but it doesn't get stored in the table. It shows customer_id = nil in the projects table.
Can someone help. Thanks :)
Do check that you added customer_id in attr_accessible method like,
class Project
attr_accessible :your_other_attributes, :customer_id
end
So here's the Thing:
I have a Rails App with "Productos" and "Ventas" Both resources have the same attributes on their tables and ventas has one more (quantity)... the models look like this:
#Producto Model
class Producto < ActiveRecord::Base
has_and_belongs_to_many :categorias, :join_table => :categoria_productos
attr_accessible :color, :existencia, :nombre, :precio, :talla, :uniclave, :categoria_ids
#Venta Model
class Venta < ActiveRecord::Base
attr_accessible :cantidad, :color, :nombre, :precio, :talla, :uniclave, :producto_ids
has_many :productos
end
I'm using ActiveAdmin for the Admin interface and my /admin/venta.rb looks like this:
ActiveAdmin.register Venta do
form do |f|
f.inputs "Registrar Venta" do
f.input :cantidad
f.input :productos, :as => :check_boxes
end
f.buttons
end
end
The result is ALL THE PRODUCTOS are showing in the "new venta" form and I can select them, but when I create a new Venta actually, the params of "venta" save empty instead of taking the selected "producto" ones...
How can I fix this?? I want all the params of the selected "producto" to be used in the newly created "venta" fields as they share the same attributes (both models have been created with the same attributes actually)
So, ideas? ;)
:categoria_ids and : producto_ids must be as :categoria_id and :producto_id OR you must use the :foreign_key for behavior between models