:disabled field not working in fields_for - ruby-on-rails

I'm trying to add a disabled drop-down box to my table, which I will eventually make conditional.
However, disabled doesn't seem to be added to the line when it is run.
If I inspect the element in the page, manually adding disabled works, but it is not being added at run-time.
= f.fields_for(:targets, qualification.target_for(#grandfather.user)) do |builder|
%tr
%td
= builder.select :completed, qualification.level_options.map{|o| [o,o]}, :disabled => "disabled"
= builder.hidden_field :qualification_id, :value => qualification.id
= builder.hidden_field :id

Check out the API for Rails' Form Helper API
select(object, method, choices, options = {}, html_options = {})
It was adding :disabled => "disabled" to the options, instead of the html_options. This is the code to use instead (notice the empty hash for the options parameter):
builder.select(:completed, qualification.level_options.map{|o| [o,o]}, {}, {:disabled => "disabled"})

Related

Multiple attribute in collection_select in Rails

I am trying to allow for multiple value selection from a collection in a Rails form. The field is working but does not allow for multiple selections (once an alternative option is selected the previously selected is unselected). I am using Bootstrap CDN, which I don't presume is causing issues, but I may be wrong?
Can you see anything wrong with this code?
<div class="field form-group row">
<%= f.label :industry_ids, class:"col-sm-3"%>
<%= f.collection_select(:industry_ids, Industry.all, :id, :name, {:multiple => true}, size: Industry.all.length) %>
</div>
Thanks for your help.
I believe your problem is that you're putting {:multiple => true} in the wrong options hash. The method signature for collection_select looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
multiple is an html attribute of the select tag itself (here's a doc), so you want to pass that to html_options, not options. Your code looks like this:
f.collection_select(:industry_ids, Industry.all, :id, :name, {:multiple => true}, size: Industry.all.length)
Where Industry.all is the collection, :id is the value_method, and :name is the text_method, which means { :multiple => true } is getting passed to options, not html_options. Move it to the second hash and you should be fine.

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

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" } %>

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

add class to collection_select

I've looked at How do I set the HTML options for collection_select in Rails? and I'm sure I'm missing something obvious, but I can't get this to work.
My select currently looks like:
<%= f.collection_select :broadcast_id, broadcasts, :id, :to_s,
:include_blank => 'Broadcast on...' %>
and I've tried including :class => 'prevent_collapse', which does nothing, as well as {:class => 'prevent_collapse'}, which gives me an error.
If anyone can point out how to do this, I'll be super grateful!
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
=>
f.collection_select :broadcast_id, broadcasts, :id, :to_s,
{:include_blank => 'Broadcast on...'}, {:class => 'prevent_collapse'}
And what error do you have?
And does broadcast item has got :to_s method? It will return class name, as I think.
Is that field :include_blank => {}, compulsory ? I tried with :include_blank => false and it worked. I wonder if we can avoid it ?

Resources