I have two fields in my Rails application: language and word. The words table is quite large, and as such I'd like to have the selection filtered by the choice of language. Below is code from the edit view and you can see how I have hard-coded in the language in the second collection_select.
.field
= f.label :language_id
= f.collection_select :language_id, Language.find(:all,:conditions => ["supported = 't'"]),:id,:language_code, include_blank: false, :title => 'Language'
.field
= f.label :word
= f.collection_select :word_id, Word.find(:all,:conditions => ["language_id = 2"]), :id, :word, include_blank: false, :title => 'Word'
A similar question was posted on SO, but the solution provided was to use AJAX. I would prefer to reduce the database load by adding a criteria after the user can selected the language.
Rails dependent collection_select fields in form
Is it possible for the Rails code to assess the chosen value of :language_id ?
Assuming the object containing the fields is Article :
= f.select :word_id, Word.where(:language_id => #article.language_id).map {|w| [w.name, w.id]}
Related
I have created drop down list for employees.
What I want?
I want to select full Name for each one of them.
Type of form:
I use simple_form.
I actually have:
= f.input :person_id, label: "Employee", collection: #employee, prompt: "Select employee"
Result(I know, that is reference):
Before I use collection_select, but simple_form doesn't support validation for this type of collection.
Code for collection_select. This type of drop down list displays properly full name.
= f.collection_select :person_id, #employee, :id, :fullName, {prompt: "Wybierz pracownika"}, {class: "form-control"}
Update:
fullName is a method in a person.rb model.
def fullName
"#{first_name} #{last_name}"
end
Object employee.
#employee = Person.where.not(type: "Client")
The easiest way to do this is :
= f.select :person_id, options_for_select(#employees.map{|e| [e.fullName, e.id]}), {:prompt=>"Wybierz pracownika", :required => true}
It will show full names as select options and will dispatch ids as values with form.
You follow this code like below:
<%= f.collection_select(:person_id, Model.all, :person_id, :fullName,{:prompt=>"Wybierz pracownika"}, {:class => 'form-control'}) %>
You will replace your model_name.
Or
= f.input :person_id, label: "Employee", collection: #employee.fullName, prompt: "Select employee"
I think will help you
I am using the ransack gem for my rails app and have a problem with multiselect.
I have a job and a company model, a company has many jobs, a job belongs to a company.
I want to perform a search on jobs so people can see jobs from specific companies,
I have done some research and apparently this should work in the search form:
= search_form_for #q do |f|
= f.collection_select :company_name_cont, Company.all, :id, :name, {:multiple => true}, class: 'chosen-it'
= f.submit "search"
So in the view I get the list (with autocomplete from the chosen gem) but when I click on search it returns no result and I can't select multiple companies.
However, when I use a search field instead like this:
= f.search_field :company_name_cont
The search works.
Could you help me guys
Thanks a lot
You should use company_name_in instead of company_name_cont:
= f.collection_select :company_name_in, Company.all, :id, :name, {}, {:multiple => true}
More info can be found in ransack's wiki here: https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching#in
In general, we can write this as
= f.collection_select :some_id_in, Model.all, :id, :name, {}, {:multiple => true}
where some_id is a model attribute for which we have multiple select value for filter.
How would one capitalise the first letter of a list of country's in below code example?
My question is regarding simple_form, not a regular form for which one could use collection_select.
= p.input :state,
:readonly => "readonly",
:collection => DataState.where(:country => "1"),
:selected => 1,
:id => "state",
:name => "state",
:prompt => "Please choose"
The problem is the list of states is in the database and saved like "alamaba","new york"
I need it to be down cased in the database but would like to uppercase/capitalize the first letter for visual clarity.
Try this:
In your DataState model, add a method that displays a capitalized version of your state names
class DataState < ActiveRecord::Base
def capitalized_state
state.capitalize
end
end
then use this new method to list your choices
Depending on if you're trying to save the id of the state as an association or just the string, the erb would be either
<%= f.input :state, collection: DataState.where(:country => "1").pluck(:capitalized_state) %>
or
<%= f.input :state, collection: DataState.where(:country => "1"), value_method: :id, label_method: :capitalized_state %>
where the former is for the state name and the latter is for the association id.
see this: using capitalize on a collection_select
Edit: I noticed that your html does not specify what attribute of the DataState class you're using as the text for your choices. If you're using rails, you should look into the collection_select form helper.
One way of going about it, too, would be the addition of the text-transform: uppercase property of css, looking like this:
<% = f.input: state, collection: DataState.where (: country => "1"), style: 'text-transform: uppercase;' %>
I have the following models in my RoR project:
scope and project_scopes.
Project has_many :scopes, through: :project_scopes. Also project accepts_nested_attributes_for :project_scopes.
I add scopes to projects by several selects:
projects/_form.html.haml
= form_for(#project) do |f|
= f.fields_for :project_scopes do |builder|
= render 'project_scope_fields', f: builder
= link_to_add_fields 'Add scopes', f, :project_scopes
projects/project_scope_fields.html.haml
= f.select :scope_id, options_from_collection_for_select(#scopes, "id", "name"), {include_blank: true, class: "project_scopes"}
= f.hidden_field :_destroy
This successfully creates projects with all scopes. When I click edit, it renders same form and displays all scope selects, but they do not have the correct selected value.
How do I fix this?
Look at the documentation for options_from_collection_for_select: It takes 4 parameters, the last one being the selected option. You're not supplying that. Try this:
= f.select :scope_id, options_from_collection_for_select(#scopes, "id", "name", #project.scope)
or simply use the collection_select helper:
= f.collection_select(:scope_id, #scopes, :id, :name)
Try following (and I'm assuming, you're setting attr_accessible correctly):
= f.select :scope_id, #scopes.map{|s| [s.name, s.id]}, {include_blank: true, class: "project_scopes"}
Btw - Scope may not be best model name.
I am looking to create 3 separate collection_selects to choose players for a team, where Team has_many players. However, if I have the following code 3 times...
= f.collection_select :player_ids, #season.players, :id, :name
... it only sends one back in the params, since they all point to the same thing.
How can I use three collection selects to add three has_many values?
I do not want to use
= f.collection_select :player_ids, #season.players, :id, :name, {}, { :multiple => true }
You're getting only one in the params, because all three dropdowns share the same html <select> element ID and name.
If you don't like the multiple approach, the only thing I can think of is specifying manually the dropdown IDs and names as follow:
f.collection_select :player_ids, #season.players, :id, :name, {}, {:id => "dropdown_1", :name => "season[player_ids][]"}
f.collection_select :player_ids, #season.players, :id, :name, {}, {:id => "dropdown_2", :name => "season[player_ids][]"}
f.collection_select :player_ids, #season.players, :id, :name, {}, {:id => "dropdown_3", :name => "season[player_ids][]"}
The IDs can be whatever you prefer but all three must be different. The real important thing is the "name" attribute, as it must match the model you're updating (in this case I suppose it's season) and having the final brackets [] will indicate it's an array of IDs (one ID per-dropdown)