How to use select method in form_for in Rails - ruby-on-rails

I would like to use a select method in form_for in Rails. I'm not quite sure how to do it. Could anyone point me to an example?

Are you talking about select or select_tag or collection_select?
# advanced users only
= f.select :field_id, options_for_select(Model.collect{|m| [m.name, m.id]})
# easiest, assuming you have a model
= f.collection_select :field_id, Model.all, :id, :name
# without model
= select_tag 'model[field_id]', #model.field_id, options_for_select(Model.collect{|m| [m.name, m.id]})

Related

Ransack Gem Rails and multiselect

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.

Rails collection_select vs f.collection_select

I can't understand the difference between the two. Can someone please explain the difference when using form_for?
Say you have this: <%= form_for(#post) do |f| %>
Examples- When would you use this?
<%= collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
vs. use this?
<%= f.collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
Rails Api
When you use f.some_form_helper the helper will already know the name of the model you want to make the field name for. This way you can drop that :post argument. form_for(#post) gives you the f form builder object that knows what model the form is for.
With the regular collection_select (or any other helper with f.) you have to pass in, as the first argument, the name of the model the field is for.
Your example is a bit off because you passed in the same arguments to both. f.collection_select doesn't need the :post.
This is correct use of the non f. helper:
<%= collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
This is a corrected way to use the f. helper:
<%= f.collection_select(:author_id, Author.all, :id, :name_with_initial, prompt: true) %>
the f object has a reference back to the model you passed in to form_for via f.object. This is how it knows to call collection_select(:post, ...) under the hood.

Rails form dropdown using two text_method

I am creating a form has a drop down selection. I want to use two "text_method"s for the input but I am unsure how to do this. I want to include the year and name (both are two different columns in my rails model.
Here is what I have but it does not work:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year, :model_name, include_blank: true %>
Here is the official documentation- http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
Use this in your view:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year_and_name, include_blank: true %>
Add a method like this to your Bat model:
def model_year_and_name
"#{model_year} #{model_name}"
end

Rails habtm nested select doesn't has right selected value on edit

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.

Ruby on Rails: How to sort a collection_select

I want to sort / order it (desc or asc how i want it) by the database table column "plays"
Im totally confused. Just found a solution for select but not collection_select?
some code of my view
<%= f.collection_select :player1, Player.all, :id, :name %>
dont know how to sort / order
there are also columns in the database table like "plays", "goals" ...
Just pass actually ordered collection to collection_select helper:
collection_select(:post, :author_id, Author.order('created_at DESC'), :id, :name_with_initial, :prompt => true)
So, in your source example it will look like this:
<%= f.collection_select :player1, Player.order('plays DESC'), :id, :name %>

Resources