collection_select onchange event and selected ruby on rails - 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

Related

Showing which option is selected in rails dropdown

I have this code:
%tr
%th Parent
%td
%select{ :name => "firstlevel_id", :value => "#{#s.firstlevel_id}" }
- Firstlevel.find(:all).each do |f|
%option{ :value => "#{f.id}" } #{f.title}
but even if the firstlevel_id is already one of the id's in the options list, it doesn't show it as selected.
You can use select_tag helper to create select tag and control selected value.
You can also set selected attribute on proper option tag:
%select{:name => "firstlevel_id"}
- FirstLevel.find(:all).each do |f|
%option{:value => f.id, :selected => f.id == #s.firstlevel_id} #{f.title}
I would prefer the first solution.
You should use the select_tag to generate the html , and options_for_select to generate the html :
select_tag :firstlevel_id, options_for_select( Firstlevel.all.map{|l| [l.title, l.id] }, #s.try(:firstlevel_id) )

append hardcoded value in f.select_tag in rails 2

I have a dropdown in my rails partial which onchange calling a remote function...I am using rails2.3 verison.. In the drop down I am retreiving list of students like this..
<%= select :students, :id,
#students.map {|s| [s.name, s.id] },
{:prompt => "Select A Student"},
{:onchange => "#{remote_function(
:url => { :action => 'type' },
:method => 'get',
:with => "'stud_id='+value",
:before => "Element.show('loader')",
:success => "Element.hide('loader')" )}"} %>
This is working fine... I get list of students with their ids as values.. But I need to add one more option as "All students" to the dropdown so that I can view the details of all the students. How would I achieve it. Please put your thoughts on this as I am new to rails and learning it...
You can do something like this. Instead of using
#students.map {|s| [s.name, s.id] }
you can simply add a blank item to the array
[["All students",'']] + #students.map {|s| [s.name, s.id] }
I forget if Rails 2 allows you to do an { include_blank: true }, it is available in later versions.

How to disable a collection_select dropdown in ruby

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);

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 2.1.2 collection_select with an :onchange and a :with

After a lot of searching and reading I've got a collection_select that looks like so
<%= collection_select :selection, :level, User::LEVELS, :to_s, :to_s,{:with => "this.value"},
{:onchange => remote_function(
:url => {:action => "updatelevel", :controller => "user", :id=> user.id})
} %>
However its not passing the selected value to my controller, the only thing I've ever got is a nil.
I've messed about with difference combinations of where :with should be and tried test strings but it never seems to do anything.
Am I missing something stupid? Is there a "definative" example I should be looking at?
Rails seems to change so fast that its hard to know which version a forum post is talking about and the api I read for collection_select doesn't show what I can put in the options hash.
I checked this out on my app running Rails 2.3.10. You have your 'with' parameter in the wrong spot, it is an option for the remote function, not for the collection select. Also, passing the value in that manner will get you a params hash that looks like {"134523456" => ""}, which probably isn't what you want. You have to have your 'with' value result in a string that is JavaScript centric.
<%= collection_select :selection, :level, User::LEVELS, :to_s, :to_s, {},
{:onchange => remote_function(
:url => {:action => "updatelevel", :controller => "user", :id=> user.id},
:with => "'level_id='+this.value"
)
}
%>

Resources