Rails 3 - select tag - onchange - ruby-on-rails

I am trying to do select box, where after select some option value will be this value send. Now I have following
<%= select_tag "action", options_for_select([ "1", "2", "3"]) , {},
:onchange =>remote_function(:url => {:action => :index, :id => 0 },
:with => "'action='+ +this.options[this.selectedIndex].value") -%>
and getting error wrong number of arguments (4 for 3). What is there wrong?
EDIT: Can anyone help me please with any function demo or example of form with select, that works with :onchange event (e.g. what should be in controlller)?
I still can't find the right way how to do. :/

Just remove the third argument {}.

Related

Can't add ":include_blank => "Please select"" to Rails 3.2 option select language(Beginner)

Beginner question
I'm trying to add {:include_blank => "Please select"} in line select.
%label Please choose the language you speak
.user-profile-select
%select{id: "user_language_language_id"}
- Language.active.each do |lang|
%option{value: lang.id}= lang.to_label
%input{type: "button", value: "ADD", id: "add_user_language"}
%span.note You can choose few
:coffeescript
$("#add_user_language").click ->
$.post #{my_user_languages_path.to_json},
format: 'js'
user_language:
language_id: $("#user_language_language_id").val()
false
I'm getting haml error or stay without changes. Who can explain what i'm doing wrong?
%label Please choose the language you speak
.user-profile-select
%select{id: "user_language_language_id"}
%option{class: "prompt"} Please Select
- Language.active.each do |lang|
%option{value: lang.id}= lang.to_label
%input{type: "button", value: "ADD", id: "add_user_language"}
%span.note You can choose few
You can provide default "option" tag before the each loop.
You can use form helper methods (select), where you can pass an array and a prompt value.
select("lang", "lang_id", Language.active.collect {|p| [ p.name, p.id ] }, {:prompt => 'Please Select'})

call a controller and method on select change with rails

i want to call a AJAX query on select change.
I found this code fragment, but it doesnt work on rails 4, or im doing it wrong
<%= f.select(:resource_id, Resource.all.collect {|resource| [ resource.name, resource.id ] }, :class => "medium", :onchange => remote_function(:url => {:controller => 'controller', :action => 'action'})) %>
and im geting this error
undefined method `remote_function' for #<#<Class:0x007f3daff24a88>:0x007f3d9c670210>
any ideas what im doing wrong ?
This should give you a hint:
<%= f.select(:resource_id, Resource.all.collect {|resource| [ resource.name, resource.id ] }, :class => "medium" %>
#javascript code to make an Ajax call:
$('select#your_model_name_resource_id').on('change', function(event) {
var selected_resource_id = $(this).val();
$.ajax('/relative/path/to/your/action', data: { id: selected_resource_id })
})
When a user change the value of the select, it should perform an Ajax request to your server at this location: /relative/path/to/your/action?id=12 (if id selected was twelve)
The remote_function has been deprecated and removed from rails. You need to write some javascript now (very simple though).
Unless you are willing to install prototype-rails gem, which will add this helper back to your rails.

Rails Multiple Select boxes: Injecting default values from params

I currently have a multiple select box in a rails form that looks like this:
= select_tag :in_all_tags, options_from_collection_for_select(Tag.where(:project_id => #project.id), :id, :name, #in_all_tags_param), { :id => "tags", :tabindex => "3", "data-placeholder" => "Choose Tags", :multiple => "multiple" }
Where
#in_all_tags_param = params[:in_all_tags]
The problem is, #in_all_tags_param will only populate the select form with the last value from params[:in_all_tags]. So, if the url string reads in_all_tags=5&in_all_tags=8, the pre-selected value in the multiple select will only be 8.
From what I understand, the way around this is to append [] to the field name for multiple params, so that :in_all_tags becomes in_all_tags[]
BUT, when I try this, submitting the form returns:
Expected type :default in params[:in_all_tags], got Array
Any suggestions appreciated.
Cheers...
You need to add a :name element to the same hash with :multiple => true in it. So I use something similar for Genres on an app for mine and I do { :multiple => true, :name => "lesson[genre_ids][]" }. The name has to be model[attribute][].

haml and no javascript? (rails: form_remote_tag and :with parameter)

i am trying to call a remote method to update page content via ajax/js.
either i am too tired already or haml is not parsing the following code correctly to send the value of the query field via prototype. any ideas?
- form_remote_tag(:url => {:controller => "search", :action => "line"},:with => "'query=' + $('query').value" ) do
%input{:type => 'text', :id => 'query'}
%input{:type => 'submit', :value => 'Search'}
thanks a lot!
t
Have you tried a
= form_remote_tag
instead of
- form_remote_tag
I'm new to HAML myself but I was under the impression that you'll need the form tag to be actually generated not just executed...
Try passing the :with as part of the options hash.
- form_remote_tag({ :url => {:controller => "search", :action => "line"}, :with => "'query=' + $('query').value" }) do
If that doesn't work, debug the problem: Look at the generated html. Is the text field with id query the only element in the page with that id? Is the js code correct? Use the Firebug console to ensure $('query').value returns whatever you've entered into the text field.
Still stuck? Add your generated html into your question so we can better help.
EDIT: Your query input tag does not have a name attribute. Without a name, the javascript helper code skips that field when serializing the form fields...also, you do not need the :with code.
%input{:type => 'text', :id => 'query', :name => 'query'}

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