How to disable a collection_select dropdown in ruby - ruby-on-rails

Is it possible to have the collection_select dropdown be unclickable(disabled)? I would like the collection_select to initially have a selection displaying but be disabled and then when some other button is clicked, the collection_select is re-enabled again(via javascript) and user can now scroll through the dropdown and click on something else. I tried :disabled => true like in the following but this did not work for me:
Embedded ruby in my html
<%=
collection_select(
:post,
:post_name,
Post.all,
:post_name,
:post_name,
{:selected => Post.where(:p_address => #parentpost.p_address).select("post_name").first.post_name,
},
{:id=>'post_collection_select',
:onchange => "DoStuff(this.value); return false;",
:autocomplete => "off",
:disabled => true
}
)
%>
So far adding the :disabled => true does nothing for me. The collection_selection is behaving exactly as it was before which is the following: it displays many post names in the drop down and one is selected based on the ActiveRecord query provided

Use
:disabled => 'disabled'
instead of
:disabled => true
Then when you want to enable the select box use the following jQuery command:
$('#post_collection_select').prop('disabled',false);

Related

Ruby on rails collection_select. Multiple and remote not working together

Ok, I have a multi select box controlled by chosen jquery plugin.
I can get multi select working without ajax, and ajax working without multiselect, but not the two together.
Here multi select works, but reloads the whole page when an item is selected (remote not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {}, { :multiple => true, remote: true, onchange: "this.form.submit();" } %>
Here ajax works fine, reloading my list only, but I can only select one option at a time (multiple not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :multiple => true, :remote => true, onchange: "this.form.submit();" }} %>
I want to be able to multi select, and with each new addition, to send an ajax request and reload the list.
Any advice on linking everything would be great! Thanks!
Figured it out:
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :remote => true, onchange: "this.form.submit();" }, :multiple => true %>

How do I set a selected option in this Rails 2 selection form?

I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:
View Select Field
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(#itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
Form Helper
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
I have tried making the view look like this:
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
But I get wrong number of arguments (2 for 1)) for that line. Also tried
<%= f.select :start, options_for_select(#itinerary.locations), {:selected => #newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!
Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?
You could try something like this in your helper
def options_for_select(locations, selected=nil)
locations.map do |l|
tag_options = location_option_attributes(l)
if l == selected
tag_options[:selected] = "selected"
end
content_tag "option", l.masterlocation.name, tag_options
end.join("\n")
end
then call field like you were trying before.
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
You can pass one more parameters to select the value like this:
<%= f.select :start, options_for_select(#itinerary.locations), :selected => #newsavedmap.start, {:include_blank => true}, {:id=>"startdrop" } %>

rails input selected values from params to select_tag (multiple => true)

I want to keep the select_tag(:multiple => true) options to be selected which were selected by user once search is performed
<%= select_tag 'values[]', method_for_options_for_select, :class => 'some-class', :multiple => true, :size => 6 %>
Suppose a user select 4 values from the select tag then for values should be selected,
How can we pass this 4 values to the select_tag?
I tried using :selected => params['values[]'] but this doesnt works for multiple true
Any help will be appreciated
Ref this and options_for_select
Something like following
<%= select_tag 'values[]',
options_for_select(#stores.map {|s| [s.store_name, s.store_id]},
#user.stores.map {|j| j.store_id}),
:class => 'some-class', :multiple => true, :size => 6 %>

rails 3.2 select form helper set readonly true not working

I am having trouble setting readonly => true on a form helper. It still allows me to change the selection. Here is my code.
<%= f.date_select :date_of_birth, {:order => [:day, :month, :year]}, {:readonly => true} %>
The readonly option does not exist in the HTML <select>-tag or <otption>-tag and even Rails cannot magically add such an option to HTML..!
Instead, you can use :disabled => true, to show the select fields as 'disabled':
f.date_select :date_of_birth, {:disabled => true, :order => [:day, :month, :year]}
Values of disabled fields are not send with the form though, so if this is required, you need to add a copy of the field as hidden as well.
This hidden field/value is then send with the form, and the disabled field is shown in on your page.

collection_select onchange event and selected ruby on rails

I am learning ruby on rails. In my project
I use collection_select. My code is
<%= collection_select(:sport_name,count,Sport.find( :all, :order => 'id' ), :id, :sport_name, {} ,
{:selected => ps.sport.id,
:include_blank => "Select Sport",
:onchange => "hidvalue("+ps.sport.id.to_s+","+count.to_s+")",
:style => "margin:1px 0 0;width:210px;" }) %>
onchange works - selected doesn't work
If I instead do
<%= collection_select(:sport_name,count,Sport.find( :all, :order => 'id' ),:id, :sport_name,
{:selected => ps.sport.id,
:include_blank => "Select Sport",
:onchange => "hidvalue("+ps.sport.id.to_s+","+count.to_s+")" },
{:style => "margin:1px 0 0;width:210px;" }) %>
onchange doesn't work, but selected works. I want to use onchange and selected together. What is wrong with this code?
Well, "selected" is an option, but "onchange" is an HTML attribute that you want to assign to the generated HTML. Those two different types of things are supposed to be passed in to collection_select inside different arguments.
In particular, "selected" should be passed in as key/value pair in the fifth ("options") hash, while "onchange" should be passed in as part of the sixth ("html_options") hash.
See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select for more information

Resources