Rails select tag with required true - ruby-on-rails

I have the following select tag
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:required => true}, {:class => "multiselect", :multiple => true} %>
I try to add :required => true to it, the view renders but :required => true doesn't work!.

Quite a bit late, but for anyone looking, checking at the reference, should go explicitly as the hash, so ruby interpreter doesn't get confused:
select(object, method, choices = nil, options = {}, html_options = {}, &block)
For this special case:
<%= f.select :id,
User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]},
{:prompt => 'Select something'},
{:required => true, :class => "multiselect", :multiple => true} %>

Try this:
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:multiple => true}, :class => "multiselect", :required => true %>

Related

rails form select multiple gives empty first value

I have defined has_and_belongs_to_many associations between Meals and Recipes. In the Meals create form, I am using a select to populate the recipes.
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {}, :multiple => true %>
But the result set has a nil as the first value.
"recipes"=>["", "2", "7"]
How can I eliminate the empty/nil value?
For me setting :include_hidden => false is what worked
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {:include_hidden => false}, :multiple => true %>
You can reject blank option by passing :include_blank => false
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {:include_blank => false}, :multiple => true %>
And you can set an prompt as following
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {:include_blank => "Please Select"}, :multiple => true %>

Simple_Form: Make two forms work with eachother (dropdown and search)

I have two forms, one containing a dropdown where the user can choose how a list is beeing sorted, the other one containing a searchfield, where the user can search through that list. Now if a user searches for "test" and ten results show up, I want the user to be able to choose from the dropdown, how the results are beeing sorted. Accordingly if he sorts the whole list, I want himto be able to search through the list, with the results showing up in the sorted way he choose before. Due to code restrictions I have to keep those two inputs in different forms.
Here is the sort-dropdown:
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
And here is the search:
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
Is it possible to keep the two inputs seperate or would it be way easier to use just one form?
You can use separate forms if you need to, just store the other parameter in a hidden field in each of the forms.
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
= f.input :q, as: :hidden, input_html: { :name => :q, :value => params[:q] }
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
= f.input :sort, as: :hidden, collection: [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')

Rails select tag in form doesn't populate with value in the edit view

I have the typical rails _form generated by scaffold for a member model. For the param :state I am using a select tag:
<%= f.select :state, options_for_select(us_states), { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
Using a helper method (us states):
def us_states
[
['AK', 'AK'],
['AL', 'AL'], #etc
And for the param "member_since" I am using the select_year helper:
<%= select_year(0, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
Now both of these selects work to create a new record, but neither field is pre-filled in the edit record view. Any thoughts?
<%= f.select :state, us_states, { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
and
<%= select_year(f.object.member_since, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
UPDATE:
Since member_since is a string, you will need to convert it to date:
<%= select_year(Date.new(f.object.member_since.to_i), {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>

rails date_select class won't take effect

I have the following use of date_select..
<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :html => {:class => "select birthday"} %>
But the class does not show up in the html..
<select id="profile_birthday_2i" name="profile[birthday(2i)]">
<select id="profile_birthday_3i" name="profile[birthday(3i)]">
I also tried..
<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :class => "select birthday" %>
That did not work either. Any ideas?
The HTML options are a fourth argument to the date_select method, rather than being a key in the third argument.
From the documentation:
date_select(object_name, method, options = {}, html_options = {})
So you'd want:
f.date_select :birthday, { :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' } }, {:class => "select birthday"}
You need to use html_options, not html to specify the class.
I believe this will work, though I've not tested it.
<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :html_options => {:class => "select birthday"} %>
See the API description here:
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
Note: The docs say:
If anything is passed in the html_options hash it will be applied to every select tag in the set.
So make sure you expect that class to show up on each element.

Rails - disable option in select (based on the condition)

I have select:
= f.select(:category_id, #categories, :html_options => {:class => 'select_box'}, {:disabled => true if category.id == 18})
The piece of code above obviously returns an error, but how to disable an option according by id?
Haven't tested this but in your controller could you not do
#checkvar = #category.id == 18 ? true : false
then in the view
f.select(:category_id, #categories, :html_options => {:class => 'select_box'}, {:disabled => #checkvar})
or in the model write a function to test
def disable_select
if self.id == 18
true
else
false
end
end
then in the view
f.select(:category_id, #categories, :html_options => {:class => 'select_box'}, {:disabled => #category.disable_select})
<%= f.select :status, STATUSES.map{|s| [s.titleize, s]}, { disabled: DISABLED_STATUSES.map{|s| [s.titleize, s]} %>
Ran into this recently and used the following solution:
#view
= f.select(:category_id, #filtered_categories, :html_options => {:class => 'select_box'}
#controller
#filtered_categories = Category.all.select do |category|
[logic here]
end
For select_tag, we can do:
<%= select_tag "sample", options_for_select(
[['A', 'a'], ['B', 'b'], ['C', 'c']]),
{class: 'form-control', disabled: data.present? == false ? true : false}
%>

Resources