Show the selected value of the dropdown after reload - ruby-on-rails

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();") %>

Related

RoR: select_tag How to pass a list of selections to controller method on button click

I have a multi select list:
<%= select_tag "selectedSuites", options_for_select(#suitesInSystem), {:multiple => true, :class => "chzn-select", :style => "width:100%;", :style => "height:100%"} %>
I now want to add a button to the same view, which will pass the list of selected options in my listbox back to a controller function, for some server side processing:
<button id='SyncButton' onclick="<%=controller.dostuff(selectedSuites_tag_selections)%>">Sync</button>
Can someone advise what I've failed to spot so far?!?
Thanks

Rails 4.0 : Selected value for form_for [select]

When submitting the below form, parameters are available in the controller with params[:teachers], but in the view (same view is rendered), selected value for "language_id" isn’t params[:teacher][:language_id], but the default one (first option).
<%= form_for :teacher , :url => {:action =>"search_teacher"} , :html => { :method => "post"} do | f | %>
<%= f.select :language_id , t('languages_hash'), :include_blank => false %>
<%= f.submit :value => t("search_button") %>
When debugging with the debug method in the view, « params[:teacher][:language_id] » parameter is present.
Thank you
By what I understood from your question, you are trying to get the previously selected language to remain selected after the form submission. For that you may need to pass a selected option in the f.select.
<%= f.select("Language", "language_id", Language.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true, :selected => params[:language_id] }) %>
This may do the trick.
For more option you can go through this link
I'm guessing that t('languages_hash') isn't returning data in the proper format that the select tag is expecting.
Check out the docs - I'm guessing you'll want to wrap that in an options_for_select call, and possibly change the order of what is returned from t('languages_hash').

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 :onchange event firing before the select loses focus

I have a few select elements in my views that are supposed to fire after the user chooses an item from the dropdown.
My selects look like this:
<%= collection_select(:project, :id, projects, 'id', 'name', { },{:style => "width:150px", :onchange => "document.getElementById('project_btn').click()" }) %>
<span class="control_button_light" style="display:none;"><%= submit_tag 'jump_to_project', :id => "project_btn" %></span>
<%= observe_field("project_id", :frequency => 1, :function => "document.getElementById('project_btn').click()") %>
The problem is that the observe_field function is firing before the select loses focus. In other words, the submit element is "clicked" 1 second after the select gets focus, even if the user hasn't finished choosing from the dropdown.
Anyone know how to delay the observer from clicking the submit until after the select loses focus?
Try this.
<%= observe_field("project_id", :frequency => 1, :function => "submit_it") %>
<script>
function sumbit_it{
setTimeout(function() { $('project_btn').click(); }, 1250);
}
</script>
After rereading the question, I don't think you need the observe_field, since you already have onchange for that field.

Resources