Collection_select how to get the selected value and save in Rails - ruby-on-rails

I want some help. i'm doing one small functionality in rails4.0.0. i have model named user and another model team and i have another model Assign . now in assign model ,I have two select boxes i able to get the usernames and teamnames from both models in form using
<%= collection_select(:team, :team_id , Team.all, :id, :name, options ={:prompt => "-Select a user"}) %>
now i need to get the selected value from this select box and store in assigns table when i submitting the form..
i don't know how to get this selected value from controller??
class User < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
has_many :users
end
class Assign < ActiveRecord::Base
belongs_to :team
belongs_to :user
end

Your object name is :assign, not :team.
collection_select(:assign, :team_id , Team.all, :id, :name, options ={:prompt => "-Select a user"})
This should create the proper names for those boxes, so they will be a part of params[:assign]. If you are using form_for #assign do |f|, it is even better to use:
f.collection_select(:team_id , Team.all, :id, :name, options ={:prompt => "-Select a user"})
as this should automatically select the right option.

Related

How to properly display value in edit form while using bootstrap3_autocomplete_input and association

I am trying to use auto-complete/type-ahead feature provided by following Rails gem https://github.com/maxivak/bootstrap3_autocomplete_input , together with https://github.com/plataformatec/simple_form
Everything works fine in case I choose "new" action for new record in form. I am able to choose value in input field by auto-complete feature. Problem is in case i choose "edit" action to edit already existing record. In that case field does not show correct value (pre-filled by form), but it shows something like: #<Airport:0x007f98b478b7a8>
Even in "show" action, I can see correct value displayed.
I tried to change f.input with f.association as I had it before I started implementing auto-complete, but this did not helped.
Records in Cargo model have correct airports_id reference stored, I checked that manually in rails console.
Question is how can I get correct Airport value pre-filled by form in case I choose "edit" action, instead some kind of reference, I got.
Rails 4.1.7
My code is:
Cargo model:
class Cargo < ActiveRecord::Base
belongs_to :airport
...
Cargo view:
...
<%= f.input :airport, :as => :autocomplete, :source_query => autocomplete_airport_city_airports_url %>
...
Airport model:
class Airport < ActiveRecord::Base
has_many :cargos, :dependent => :destroy
attr_accessible :iata_code, :name, :city
validates :iata_code, :name, :city, presence: true
validates :iata_code, :uniqueness => { :scope => :name }
validates :iata_code, length: { is: 3 }, format: { with: /\A[a-zA-Z\d\s]*\z/ }
validates :name, :city, length: { minimum: 2, maximum: 128 }
def full_airport_name
"#{city} / #{iata_code}"
end
end
Airports controller
class AirportsController < ApplicationController
autocomplete :airport, :city, { :display_value => 'full_airport_name', :full_model=>true }
...
Routes:
resources :airports do
get :autocomplete_airport_city, :on => :collection
end
Actually I found the problem. First of all I refactored Airports model, removed all columns but name, and reseed name column with data concatenated from separate strings IATA code / City. After this, there is need to specify in model, what to show as value. Simply this solved this issue:
class Airport < ActiveRecord::Base
has_many :cargos, :dependent => :destroy
attr_accessible :name
validates :name, presence: true
validates :name, :uniqueness => true
def to_s
name
end
end
This is described, I didnot understand it on first sight previously, in original documentation here https://github.com/maxivak/bootstrap3_autocomplete_input section Model.
User f.association and because rails will automatically look for :nameand you do not have that, you'll have to define it like so:
f.association :airport, label_method: :full_airport_name, value_method: :id........etc

Ruby on Rails has_and_belongs_to_many uninitialized constant

I'm building a site on Refinery CMS, and have generated two extensions: one for Brands, and another for Bicycle Types (it's a site for a bike shop).
Now, what I want to do is have the Brands extension handle the creation of brand pages, which will be pulled into a brand index. On this page, I want to be able to filter by Bicycle Type, which is where the second extension comes in. Through the Bicycle Type extension, you can create a bicycle type, which I want to associate to a Brand. A Brand can have multiple Bicycle Types, and vice versa.
So, I edited the Brands model to add has_and_belongs_to_many :bicycle_types, and the Bicycle Types model to include has_and_belongs_to_many :brands and accepts_nested_attributes_for :brands. I wrote a migration to create a join table, and everything was going well so far.
I then went to modify the form for the Brands extension, and got my checkboxes displaying correctly and seemingly generating the right code. However, the problem occurs when I come to submit the form - I get NameError in Refinery::Brands::Admin::BrandsController#update and uninitialized constant Refinery::Brands::Brand::BicycleType.
The parameters I get look like the bicycle type IDs are being passed through correctly:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"3193ZMPXkmHdgZThXwAurD6xF2eZ533Tb71pAi7Jxbs=",
"switch_locale"=>"en",
"brand"=>{"title"=>"Cannondale",
"teaser"=>"",
"splash"=>"",
"details"=>"",
"introduction"=>"",
"blockquote"=>"",
"bicycle_type_ids"=>["1",
"2"],
"logo_id"=>"",
"teaser_image_id"=>"",
"splash_image_id"=>""},
"id"=>"2",
"locale"=>:en}
I've been trying to figure this out and just keep hitting the same brick wall, so any help would be greatly appreciated!
Here's my code. Let me know if anything else would help.
Brands Controller
module Refinery
module Brands
module Admin
class BrandsController < ::Refinery::AdminController
crudify :'refinery/brands/brand',
:xhr_paging => true
end
end
end
end
Brands Model
module Refinery
module Brands
class Brand < Refinery::Core::BaseModel
self.table_name = 'refinery_brands'
attr_accessible :title, :teaser, :splash, :details, :introduction, :blockquote, :logo_id, :teaser_image_id, :splash_image_id, :position, :bicycle_type_ids
translates :title, :teaser, :splash, :details, :introduction, :blockquote
class Translation
attr_accessible :locale
end
validates :title, :presence => true, :uniqueness => true
belongs_to :logo, :class_name => '::Refinery::Image'
belongs_to :teaser_image, :class_name => '::Refinery::Image'
belongs_to :splash_image, :class_name => '::Refinery::Image'
has_and_belongs_to_many :bicycle_types
end
end
end
Bicycle Types Model
module Refinery
module BicycleTypes
class BicycleType < Refinery::Core::BaseModel
self.table_name = 'refinery_bicycle_types'
attr_accessible :title, :position
translates :title
class Translation
attr_accessible :locale
end
validates :title, :presence => true, :uniqueness => true
has_and_belongs_to_many :brands
accepts_nested_attributes_for :brands
end
end
end
Migration
class AddRefineryBicycleTypesBrands < ActiveRecord::Migration
def change
create_table :bicycle_types_brands, :id => false do |t|
t.references :bicycle_type
t.references :brand
end
add_index :bicycle_types_brands, [:bicycle_type_id, :brand_id], :unique => true
end
end
Form Partial (at least the part where I'm building my checkboxes)
<div class="field">
<%= f.label :bicycle_types %>
<% Refinery::BicycleTypes::BicycleType.order(:title).each do |bicycle_type| %>
<label class="checkbox">
<%= check_box_tag "#{f.object_name}[bicycle_type_ids][]", bicycle_type.id, f.object.bicycle_types %>
<%= bicycle_type.title %>
</label>
<% end %>
</div>
If the rest of the partial would be useful, or anything else for that matter, please let me know. Any help would be greatly appreciated!
you must specify the full class name:
has_and_belongs_to_many :bicycle_types, :class_name => "Refinery::BicycleTypes::BicycleType"
This is not your case but if you want to call the join table in a 'refinery style' (i.e refinery_bicycle_types_brands), you must also declare the join table:
has_and_belongs_to_many :bicycle_types, :join_table => :refinery_bicycle_types_brands, :class_name => "Refinery::BicycleTypes::BicycleType"
Bye

Rails Format Collection_Select Based on two relationships

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

Rails: Pass "params" using Checkbox

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

Rails table inheritance issue

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

Resources