f.select onchange , pass this.value to a ruby variable - ruby-on-rails

I have got this line of code in form:
f.select(:SUB_ASSAY_TYPE, #types, {:prompt => 'Select the Type'}, {:onChange => "alert(this.value)}) %>
What i want to do is to assign the 'this.value' to a ruby variable on onchange event like shown below:
{:onChange => "alert(this.value); #rubyvar = (this.value)" }
I know that's not how it should be done but i have no idea how to do this in ajax or using remote function.
Many thanks for your help

There is no way to set a ruby variable from the client, since its evaluated on your server.
What you need is an Ajax call which renders some new javascript to the client.

Related

select2 gem remember selected options after search

I have used select2 gem in my rails 6 project to show multiple options to select
<%= select_tag 'skill[]', options_for_select(Course.populate_data, :selected=> "#{params[:skill] rescue nil}"), :prompt => "skill", class: "form-control js-select",id: "js-skill", :style => "width:180px", multiple: "multiple", :style => "width:280px;"%>
I want selectbox to remember my last selections after I submitted the search form
I tried data amd params, but not working for me, can anyone help me with this
I solved the issue, using below code. I was not doing AJAX call, otherwise I would have done this in success response of query
$(".js-select").select2();
if ("<%=params[:skill]%>") {
var a = <%=raw params[:skill]%>
$('#js-skill').select2().val(a).trigger('change');
} else {
$("#js-skill").select2();
}
Posting the answer, if it may help someone

select onchange updates another select with remote_function

I use Rails 3.0.3
I've 2 <select>, when the user choose an option in the first <select>, the second should be updated. The onchange event on the first select fire an ajax request to get the list(JSON format), the success event call a function that transform the JSON to a list of <option> to be inserted in the second <select>.
Here the first <select>
<%= collection_select :brand, :category_id, Category.all, :id, :name, {}, {:onchange => "#{remote_function :url => brands_by_category_path(:category_id => ???), :success => "buildOptions(request, 'contract_brand_id')"}"} %>
the parameter of brands_by_category_path (???) should be egal to the selected option of this <select>
I really don't know how to achieve that?!!
Any idea?
Thanks
dont use this ugly inline stuff, with rails 3 (its supports 100% unobstructive javascript).
add a javascript tag after this and do this:
get this select element by id
set up an event listener for the change event
add a call back to this event
make sure the call does this: brands_by_category_path(this.selected)

RoR select_tag selected value

I'am using select_tag and I want to pass selected value in href :onchange
I couldnt fetch the selected value
Please guide me how to fetch the selected value and pass in href
I'am using following code
<%= select_tag "name",
options_for_select(#user.names.map {|c| [c.firstname,c.id]}),
:onchange =>"document.location.href='/names/value'" %>
In the code in href='/names/**value** at the place of value I have to pass selected value
Thanks
I'm not sure to understand your question but you can try this:
<%= select_tag "name", options_for_select(#user.names.map {|c| [c.firstname,c.id]}),:onchange =>"document.location.href='/names/'+this.value" %>
Thereby, the selected value is used during the onchange event.
Try this
<%= select_tag "name",
options_for_select(#user.names.map {|c| [c.firstname,c.id]}),
:onchange => "document.location.href=/names/ + $(this).value" %>
I think the suggested methods j and kiva would not work because the javascript components are rendered (written to the static html file) at the initial stage, rails is server side, and what is suggested in their posts is more client side.
I achieved it like this ->
add a onchange => myJavaScriptFunction()
and then code the url change in the javascript function()
using pure javascript, no rails involvement.
here's my snippet -
<%= select_tag :clientid, options_for_select(#options),
:include_blank => false,
:onchange => "myfunction(this)"
%>
function myfunction(combo_box)
{
//var combo1 = document.getElementById('clientid');
var value1 = combo_box.options[combo_box.selectedIndex].text;
var value2 = combo_box.selectedIndex;
document.getElementById('session_dump').innerHTML='<hr>'+value2 + '::::=> ' + value1+'<hr>';
}
Hope its useful...

How to add an onchange event to select tag in rails

How do I add an onchange event here?
Framework: rails
Database: MySQL
I am populating the options from the database and that made me use options_from_collection_for_select
select_tag(:variable,options_from_collection_for_select(:all, :id, :name))
select_tag takes an options hash as its final parameter, in which you can add any HTML attributes for the select. So to add an onchange attribute:
select_tag :variable, options_from_collection_for_select(:all, :id, :name), onchange: 'yourOnChangeHandler()'
try something like:
:onchange => remote_function(:url => {:controller => 'controller', :action => 'action'})
For a select_tag, just add:
{:onchange => "myHandler();" }
Also, if onchange doesn't work you might want to try onChange with a capital C.
Finally, make sure NOT TO CONFUSE a select_tag with a form select.
See my answer to a similar question, only regarding a form select and not a select_tag
Adding An Onchange Event To A Form Select
You may want to add an onchange attribute inline:
select_tag(:variable,options_from_collection_for_select(:all, :id, :name)), {:onchange => "(function(e){ e.target.value }).apply(this,arguments)"}
For my case, I'm not sure where to put the named functions, or sometimes I find it tedious to create a function just to create a simple tag. So people tend to write an inline function.
but a simple line like {onchange: "e.target.value"} won't work, because there are no variables (e.g. event) defined.
Solution would be a self executing function that accepts arguments

Reusing the partials multiple times on one page in Ruby on Rails

I have a partial that renders a select box using the following method:
<%= collection_select 'type', 'id', #types, "id", "name",
{:prompt => true},
{:onchange =>
remote_function(
:loading => "Form.Element.disable('go_button')",
:url => '/sfc/criteria/services',
:with => "'type_id=' + encodeURIComponent(value) + '&use_wizard=#{use_wizard}'"),
:class => "hosp_select_buttons"
} %>
This partial gets used 2 times on every page, but at one point I need to get the value of the first select box. Using:
$('type_id')
returns the second select box. Is there a way to find the first one easily? Should I fix this using javascript or by redoing my partial?
Note: the dropdowns do get rendered in separate forms.
Yes, each element does need a unique ID, the page probably also fails HTML validation. Also, unless these are in 2 different forms you'll have a conflict with the CGI parameter name.
If they are in 2 different forms you can probably get away with just setting the :id as you posted, if they are the same form you need to abstract the parameter name too:
collection_select 'type', "id_wizard_#{use_wizard}"...
I figured out one way to do this by assigning an ID in the html_options block. I already am passing in a value for use_wizard, so I can append that value on to the ID to differentiate between the two dropdowns.
:id => "type_id_wizard_#{use_wizard}"

Resources