I'm trying to disable a dropdown from being updated on a form.
Currently i have this line in my form:
<%= f.select :permission, options_for_select([['Admin', 'admin'], ['Read Only', 'readonly'], ['Editable', 'editable']], {:disabled => #permissions_disabled}) %>
With my edit controller method containing:
#permissions_disabled = params[:id].to_i == current_user.id.to_i
p #permissions_disabled
I can clearly see in my log that 1#permissions_disabled1 is true, but when i edit the form, i can still select new values in the dropdown.
What am i doing wrong here?
select accepts 5 parameters, the 4th one is a set of options for the helper. the 5th one is the html options like class and id. I think you need to pass it to that
<%= f.select :permission, options_for_select([['Admin', 'admin'], ['Read Only', 'readonly'], ['Editable', 'editable']], {}, {:disabled => #permissions_disabled}) %>
UPDATE: didn't see the options_for_select in your code. you don't need that if you're using select, you'd only want to use that when you're using select_tag
<%= f.select :permission, [['admin', 'Admin'], ['readonly', 'Read Only'], ['editable', 'Editable']], {}, {:disabled => #permissions_disabled} %>
Related
I have a ruby on rails app that has a form. I was wondering if there is a way to make sure that a user has selected drop down menu items in both of the drop downs on this form before it is submitted and params are generated. Ideally I would like to throw and error warning them to pick 1 item on each of the drop downs and re-render the form.
The form is below:
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(#products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(#products, 'id', 'name') %>
<%= f.submit %>
<% end %>
Please let me know how I can accomplish what I stated above.
SIDENOTE: I also implemented Select2 to make the form look nicer but could not find out of there is a quick validation trick in Select2 to accomplish what I said above, if there is a suggestion for that I can post the Select2 version,
Try this:
<%= select_tag :product_id1, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select...",:required=>true %>
<%= select_tag :product_id2, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select..." ,:required=>true %>
I'm trying to select multiple events in the invtime form.
Invtime
has_many :events
This is my form code:
<%= f.select :event_id, Event.all.collect {|x| [x.title, x.id]}, {}, :multiple => true %>
The display looks good! But, when I select the last event and submit, I get:
undefined method `to_i' for ["", "62"]:Array
The page inspection shows:
<select id="invtime_event_id" multiple="multiple" name="invtime[event_id][]">
<option value="66">Meeting</option>
<option value="62">Auto fill some fields on new workorder</option>
</select>
Thanks for the help!
The select helper doesn't know what to do with a collection like that, it assumes the first value is an ID and the second a string, you might be able to just reverse your method calls but this isn't really the right way to create a select tag, instead either use collection_select as dax suggests or use the options_from_collection_for_select helper:
<%= f.select :event_id, options_from_collection_for_select(Event.all, :id, :title), {}, :multiple => true %>
undefined method `to_i' for ["", "62"]:Array
from the docs:
:include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
So try adding include_blank: true
<%= f.select(:event_id, Event.all.collect {|x| [x.title, x.id]}, {}, :multiple => true, include_blank: true ) %>
also, you might try collection_select, where #events = Event.all
<%= f.collection_select(:event_id, #events, :id, :title, include_blank: true ) %>
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" } %>
Iam new to ruby on rails.I want to show the selected values from drop down and checked radio button in page reload.
When the page is reloaded, the selected value of the drop down and radio button are getting reset.So please let me know how to fix this issue.Here is the code i'am using.
<%= form_tag :action => 'show' do%>
<strong>Select device: </strong> <%= collection_select(:device, :id, #devices, :id, :name, options ={:prompt => "-Select a device"}) %>
<br></br>
<strong>Chose:</strong><%= radio_button_tag :name,:time, false, :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph%>Graph
<% end %>
Try:
collection_select(:device, :id, #devices, :id, :name, {:selected => #your_selected_value, :prompt => "-Select a device"})
see radio_button_tag(name, value, checked = false, options = {})
<%= radio_button_tag :name,:time, #your_checked_value.eql?('time'), :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph, #your_checked_value.eql?('graph') %>Graph
I've built a multi-select form (from within form_for) like this:
<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>
</div>
this works well for what I need. The only problem is that when i go back to the page that displays the choices, I don't see what was previously selected (i.e. what is already in the DB at time of rendering). Is there an easy way to get rails to display what's previously been selected? I'd MUCH prefer not to switch to check boxes.
in my matching profiles model (corresponding to the table that stores excluded_sources), i have this:
serialize :excluded_sources
this ended up being the relevant piece:
:selected => matching_profile.send(:excluded_sources)
here:
<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, :selected => matching_profile.send(:excluded_sources)), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>