Rails: Default Value - select field - ruby-on-rails

I want to display a default select value. What I have so far is:
<%= f.collection_select(:user_id, User.where(brand: current_user.brand), :id, :name, {prompt:true}, {class: 'form-control'}, :selected => current_user.brand) %>
But this produces an error:
wrong number of arguments (7 for 4..6)
Any ideas what the problem is?

This should do the trick:
<%= f.collection_select(:user_id, User.where(brand: current_user.brand), :id, :name, {prompt: true}, {class: 'form-control', selected: current_user.brand}) %>
selected should be in the html_options. See also:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

Related

Translating form_for syntax to simple_form syntax

I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).

How to make a drop-list selection mandatory excluding the blank value

In my code, i have the following drop list
<%= f.collection_select :manufacturer_id, #manufacturers, :id,
:name,{include_blank: "Select manufacturer", required: true}, {class: "form-control"} %>
Somehow the blank is accepted as a selection. Is there a way to prevent this? I want the user to select a value form the drop-list, except for the blank.
Thanks for the help!
Required is an html parameter. Try:
<%= f.collection_select :manufacturer_id, #manufacturers, :id,
:name,{include_blank: "Select manufacturer"}, {class: "form-control", required: true} %>

Include blank and default for collection_select helper

I would like to have a "select an option" option in my dropdown on my rails application.
I am using the collection_select helper tag and it looks something like this:
<%= collection_select(:country, :id, Country.order('name ASC'), :id,:name, {},{:class => "input-xlarge"}) %>
I want the default option of the drop-down to be "Select a country".
Use the include_blank option:
<%= collection_select(:country, :id, Country.order('name ASC'),
:id, :name,
{ include_blank: 'Select a country' },
{ :class => "input-xlarge" }) %>
See the official documentation here

set data attribute in grouped_collection_select dynamically in rails 4

I have a form like this in my orders/new page. I have used this to select model location and then choose a model from the location according to what is selected in the above select box.
<%= f.label "SELECT MODEL LOCATION *" %>
<div class="list_number">1</div><%= f.collection_select :location_id, Location.all, :id, :formatted_display, prompt: true, :required => true, :class => 'chosen-select' %>
<%= f.label "SELECT YOUR MODEL *" %>
<div class="list_number">2</div>
<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.find_by_location_id(group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>
In the grouped_collection_select when I use map to set the data attribute I am getting the following error:
undefined method `map' for #<Performer:0x00000006813958>
How do I solve this error and get the data required?
After trying the below code
<%= f.grouped_collection_select :performer_id, Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>
I get the following error
undefined method `performers' for [{"data-markup"=>#<BigDecimal:67e80c8,'0.0',9(36)>}]:Array
When I tried the following
<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name,Location.order(:name).map{|group| Performer.where(location_id: group.id).map{|performer| {'data-markup'=>performer.white_label.markup}}}, include_blank: true, :required => true, class: 'chosen-select' %>
I get the following error
undefined method `merge' for #<Array:0x000000069705a8>
Performer.find_by_location_id(group.id)
returns an instance but map is method for Array.
The fix depends on your business logic (and APP structure)
You can use Performer.where(location_id: group.id).map... if there are many Performers
I think you try to use this helper wrongly. According to documentation you should pass a collection and group_method. group_method - The name of a method which, when called on a member of collection, returns an array of child objects representing the <option> tags.
You pass an array of objects [{'data-markup' => some_value}, ...] as collection and performers as group_method. Rails try to call {'data-markup' => some_value}.performers and this raises an exception.
You should rewrite a collection the following way:
1. it should be array
2. each item of array should have method performers - which return an array of objects under one group item
I hope it will help you
I can't test but it can work (if Location has_many performers or you can implement your own method according to your business logic)
<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name, include_blank: true, :required => true, class: 'chosen-select' %>

f.collection_select not displaying the selected value

There are so many results for this search on google, and it's even asked at SO - but the solutions discussed so far are not helping me. Here's the issue: I have a form_for #company |f| and I am using f.collection_select for company_status_id - but when the form loads, I want the actual company status selected if it is set. Through the debugger I know, that it's been set, yet I am getting a default value displayed there. Here's the code:
= puts #company.company_status_id
= f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value}
Here's the generated htmnl
<select id="company_company_status_id" prompt="-Select-" name="company[company_status_id]">
<option value="1">-Not Available-</option>
<option value="2">Active</option>
<option value="3">Bankrupt</option>
<option value="4">Acquired</option>
</select>
And the conditions remain the same even if I do:
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => :selected => #company.company_status}
Or
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => #company.company_status}
This is what I finally did:
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => #company.company_status_id.to_i}
I read on of the answers on a similar question that collection_select automatically selects the selected value by making comparisons of what is passed with the attributes of collection. apparently there was a difference of their types, and comparing the int from CompanyStatus to the int of #company.company_status_id.to_i worked out. Though #company.company_status_id is supposed to be int as well. I can see that in the db. Anyway, it this line of code worked.
If anyone can exaplain, I will be much thankful!
If you use collection_select helper, syntax is very simple:
<%= f.collection_select :category_id, Category.all, :id, :name,
prompt: true, selected: #product.category_id %>
I hope this help
<% form_for(#company) do |f| %>
<%= f.select(:company_status_id, ListCache.all.map {|lc| [lc.name, lc.id]} ) %>
<% end %>
Sometimes you just need to go to the browser address bar and press enter. Normal reloading the page clicking the refresh button doesn't help. My problem was solved that way.
Use select_tag instead
<%= form_for(#product, :html => {:multipart => true}) do |f| %>
<%= select_tag("product[category_id]", options_for_select(#categories.map { |cat| [cat.name, cat.id] })) %>
<%end%>
Hope this help you.....

Resources