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.
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 have a drop down select that is set up like below:
<%= select_tag :city_id, option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name) %>
It works fine, except that when I load the edit view the list loads the first item in the select, not the saved value. Is there parameter I'm missing? On rails 4.
According to the documentation on option_groups_from_collection_for_select found here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select
It has a sixth parameter that is the selected value, so just add the last parameter with the value you want and it will work:
<%= select_tag :city_id,
option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name, "your_city") %>
Instead of using select_tag use select
# f being your form object
<%= f.select :city, option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name) %>
Considering you have a valid association with city
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
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]})
I'm trying to create a <select> element using the collection_select method, but it seems that in order for the proper <option> to be selected, the identifier passed into collection_select needs to be an instance variable and not a local variable (this is happening in a partial).
So when I create a <select> for the categories of a product, the proper category is NOT selected by default.
_product_row.erb (DOES NOT WORK):
My product: <%= product.name %>
<%= collection_select(:product, :category_id, #current_user.categories, :id, :name, options = {:prompt => "-- Select a category --"}) %>
Screenshot:
alt text http://img534.imageshack.us/img534/8929/screenshot20100421at120.png
I discovered that I was able to get it to work by declaring an instance variable before hand, but this seems like a huge hack to me.
_product_row.erb (WORKS):
<% #product_select_tmp = product %>
<%= collection_select(:product_select_tmp, :category_id, #current_user.categories, :id, :name, options = {:prompt => "-- Select a category --"}) %>
Screenshot:
alt text http://img534.imageshack.us/img534/1958/screenshot20100421at120l.png
Because this partial is iterating over a collection of products, I can't just have #product declared in the controller (IOW unless I'm missing something, product must be a local variable in this partial).
So how do I get collection_select to select the appropriate item when calling it with a local variable?
Have you tried passing in the :selected key in the options hash? If you provide it with the current product.id it should behave the way you're expecting.
<%= collection_select(:product, :category_id, #current_user.categories, :id, :name, {:prompt => "-- Select a category --", :selected => product.category.id}) %>
You can pass collections to partials and designate a local variable to pass them as:
<%= render :partial => "products/product_row", :collection => #products, :as => :products %>
Relevant documentation: http://apidock.com/rails/ActionView/Partials