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...
Related
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
I am trying to display the value have chosen, but it does not seem to be working. Any ideas?
Here is the code:
<label>Apply to <%= statement_display_name.downcase %>:</label> <%=
f.select( :statement_id,
options_for_select(#client.statements.unpaid.collect { |statement|
[statement_display_name + " #{statement.number_with_prefix} #
{statement.created_or_bill_date.to_date} - #
{number_to_currency(statement.outstanding_amount)}"]}), :selected
=> f.object.statement_id )%>
You're passing an array as the options, but you'll need a hash of your_string => statement_id instead to make this work.
Right now the options don't make reference to the statement ID as their value. You pass it an array of [[some_string], [some_string]], whereas you probably need {some_string => statement_id, some_other_string => statement_id}
Say we have a select box:
/app/views/tape/_form.html.erb
<%= f.select :tape, Tape::LIST_TAPES %>
and a .js.coffee file that i would like to trigger when some value is selected in selectbox:
/app/assets/javascripts/tapes.js.coffee
function selectBoxValue(value){
# value -- selected in selectbox;
console.log("box_value = ", value);
}
How can it be done in RoR 3.2?
Here's what i mean within html+js.
P.S.
I'm new to Rails. Sorry for my English and thank you.
Something like that:
<%= f.select :tape, Tape::LIST_TAPES, :id => 'some_id' %>
js file:
$(function(){
$('#some_id').change(function(){
selectBoxValue(this.value);
});
});
Pay attention that your select tag already has id so you can use it and remove :id => 'some_id'
I'm working on a legacy project that is using acts_as_taggable_on which expects tags to come in arrays. I have a select box allowing users to select a tag on a Course in a field called categories. The only way mass assignment create will work is if params looks like this params = {:course => {:categories => ['Presentation']}}. I've currently a view with this helper:
<%= f.select 'categories', ['Presentation' , 'Round Table' , 'Demo', 'Hands-on'] %>
Which will give me a parameter like params = {:course => {:categories => 'Presentation'}}. This doesn't work since Acts as tag gable apparently can't handle being passed anything other than a collection.
I've tried changing categories to categories[] but then I get this error:
undefined method `categories[]' for #<Course:0x007f9d95c5b810>
Does anyone know the correct way to format my select tag to return an array to the controller? I'm using Rails 3.2.3
I didn't work with acts_as_taggable_on, but maybe this simple hack will be suitable for you? You should put it before mass-assignment.
category = params[:course][:categories]
params[:course][:categories] = [category]
If you are only going to allow the selection of ONE tag, you could do:
<%= f.select 'categories', [['Presentation'] , ['Round Table'] , ['Demo'], ['Hands-on']] %>
Each one item array will have first for the display value, and last for the return value, which in this case will both return the same thing, as the first element of the array is the same as the last element when the array as one element.
Seems like select doesn't give you that option.
If I understand correctly, one option might be to use a select_tag instead and just be explicit about where you want the selection in the params:
<%= select_tag 'course[categories][]', options_for_select(['Presentation' , 'Round Table' , 'Demo', 'Hands-on']) %>
That ought to get your params the way you need them.
Here's what I'm using for one of my projects:
<% options = { include_blank: true } %>
<% html_options = { required: true, name: "#{f.object_name}[#{resource.id}][days][]" } %>
<%= f.select :days, DAYS, options, html_options %>
Without html_options[:name], Rails handles the name of the select tag and spits out something like
service[service_add_ons_attributes][11][days]
but I need
service[service_add_ons_attributes][11][days][]
So I override it.
Hope that helps.
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.