How to make selected option disabled in ruby - ruby-on-rails

Following is my code for select tag.
I want to disable the option that has been selected
How to achieve this?
<td>
<%= select("gu", "rm_id", Rm.where(category_id: Admin.select("category_id").where(id: current_user.id)).collect {|p| [ p.name, p.id ] }, {prompt: 'Assign RM'},{onchange: "update_rm_div(this.value,#{gu.id})"}) %>
</td>
Any help is appreciatable

Related

Add custom option (text + value) in a select tag

I'd like to add an "Other" option with the value -1 in this select:
<%= f.select :id, options_from_collection_for_select(#vehicles, :id, :model, "#used_vehicles[i]") %>
Should I replace #vehicles with something else?
If you can, you should use the already existant behaviour in Rails, which is to use the include_blank option:
<%= f.select(:id, #vehicles.collect {|p| [ p.name, p.id ] }, {include_blank: 'Other'}) %>
You can check the documentation also.

Show the selected value of the dropdown after reload

I am working on this from a very long time. I want to show the elements of a collection in a drop down, and on click of a value I must reload the page and display its details. This I am able to do easily. The problem is, When the page is reloaded, the selected value of the dropdown is getting reset. So I just want retain the selected value after the reload to use ':selected' attribute so that after relaod the clicked value is shown. So please let me know how to fix this issue ASAP. Pls Help me out with this.
<%= form_tag({},:method => :get, :class => 'formSearch absolute') do %>
<%=select("post", "id", #other_schools.collect {|p| [ p.name, p.id ] }, { :include_blank => true }, :onchange => "this.form.submit();") %>
<%end%>
As per your code,
You are searching based on select box value and you want to populate search option
I will use select_tagmore info here
<%=select_tag("post_id", options_for_colletcion_select(#other_schools,p.name, p.id ,params[:post_id), { :include_blank => true }, :onchange => "this.form.submit();") %>
You can go through docs for syntax options.
In this code where does the this.form.submit(); method will go?
<%=select_tag("post_id", options_for_colletcion_select(#other_schools,p.name, p.id ,params[:post_id), { :include_blank => true }, :onchange => "this.form.submit();") %>

How to add option value = "" in select boxed in Rails?

I want to use jQuery validation plugin for select boxes, but I don't know how to add value ="" in select. Here is my code:
<%= f.select :language, options_for_select([
"--- please select ---",
"Arabic",
"Chinese"
])
%>
I want to add
value="" option="please select"
How I can do it in Rails ?
<%= f.select :language, options_for_select([
"Arabic",
"Chinese"
], {:prompt => "please select"})
%>

Rails select options not showing

I want to create a search form for products according to category and sub-category. The user will have to select a category first and then a sub-category. The code that I have written is below:
<%= form_tag('/products/search') do |f| %>
<%= select_tag(:category_id, Category.all.collect { |s| [ s.name, s.id ]} ) %>
<%= select_tag(:subcategory_id, Subcategory.all.collect { |s| [ s.name, s.id ]} ) %>
<div><%= submit_tag 'Search' %></div>
<% end %>
but when I see the page in the browser, I see empty selects. The HTML source returns select with no options as below:
<select id="category_id" name="category_id">
jewellery1beads2pendants3</select>
<select id="subcategory_id" name="subcategory_id">necklace1earrings2taps3</select>
What am I missing?
You can just do this.
<%= select_tag(:category_id, options_from_collection_for_select(Category.all, "id", "name")) %>

Ruby on rails: Select options menu with default value attribute

I need to produce a select menu with a Default value on the list of <options> . Here is how I need it looks like.
<select name="menu[parent_id]" id="menu_parent_id">
<option value="0">==None==</option>
<option value="34">TEST</option>
</select>
Currently I use this select helper in my form
<%= f.select(:parent_id, #parent_menus.collect {|p| [ p.name, p.id ] }, {:include_blank => '==None=='})%>
the above code produce this; (value="")
<select name="menu[parent_id]" id="menu_parent_id">
<option value="">==None==</option>
<option value="34">TEST</option>
</select>
Does anyone here can show me a way to add value="0" to the options list?
<%= f.select(:parent_id, [["==None==", 0]] + #parent_menus.collect {|p| [ p.name, p.id ] }) %>
Try
<%= f.select(:parent_id, options_for_select(["==None==", 0] + #parent_menus.collect {|p| [ p.name, p.id ] }, 0)) %>
Thought I would add this to anyone looking to do a default selected value that is one of the objects in the dropdown, as opposed to a 'none' value. ie, you are editing a form that has a previous value selected, and you don't want to lose that previous value on your edit page:
Assuming you have an array of parents held in #parents and your form is tied to an object called #my_messed_up_family, and #my_messed_up_family has one 'father':
= f.label :parent_id, "Choose which of your parents is your father?
= f.select :parent_id, options_from_collection_for_select(#parents.sort_by {|n| n.name}, "id", "name", :selected=>#my_messed_up_family.father.id)
I don't know this is Ruby way or not But this will definietly work
<%= f.select(:parent_id, "<option value='0'>Please select</option>"+options_for_select(#parent_menus.collect {|p| [ p.name, p.id ] }))%>
EDITED. For pre-selected according to the value save in database i assume #user is your object contain the database value for following example.
<%= f.select(:parent_id, "<option value='0'>Please select</option>"+options_for_select(#parent_menus.collect {|p| [ p.name, p.id ] }, #user.id ))%>

Resources