select onchange updates another select with remote_function - ruby-on-rails

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)

Related

Rails select can't get both onchange and selected default to work at the same time

I am using select for a drop down menu and I want it to update the view onchange and I want it to show the default selected value to be the value that was last selected. I can get one or the other to work but not both at the same time. here is my code.
<%= select("num", "xs1", a.map {|f| [f,f], {include_blank: true}, selected: params[:num], :onchange => "this.form.submit();") %>
With this code the onchange works but not the selected part. If I change the order and put onchange first the selected part works but not the onchange. Is there a way to write this to get both to work?

How do I properly use a select box with ruby on rails?

I have a select box for the event types of an event. event belongs to event_type and event_type has many events. Here's what I have for the select box:
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :id, :name, #event.id), :placeholder => 'Select an event type' %>
But, the problem is that when I submit it, it submits the id of the event type and not the name of it. So, how would I make it submit the name rather than the id? If you need to see the any more code than just tell me, thanks
The second parameter to options_from_collection_for_select is the value that will be submitted with the form. You have :id, so change it to :name.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
(but this seems like a strange thing to do - typically you would store the event type ID.)
You can use the id after the submit to load the event type again in your controller post action like this:
selected_type = EventType.find(params[:event_type]
It is also a good practice to keep database calls to the controller, so please put the EventType.all statement in there and pass it as local or class variable like you did with event.
If you really want to pass the name in your form instead of the id, you can replace the :id part in your call to something more like this options_from_collection_for_select(#event_types, :name, :name, #event.event_type.name). Keep in mind that this value should be unique!
The method works like this:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
So the first parameter contains all the options, the second defines the value within those option objects which are put into the value field of the HTML option (which is being submitted by the form), the third defines the text which is displayed to the user and the final parameter defines the value of the selected entry (in case you are editing an entry for example). For the last parameter, you need to use the events' event_type id, or in your case, the name because you set the value of your HTML tag to it.
Use pages like ApiDock or the Rails tutorials to get examples for some of these methods.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
You should pass name in the Value method, if you want to pass the name,
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :name, :name, #event.id), :placeholder => 'Select an event type' %>
Here is the doc for options_from_collection_for_select

How to pass an array in a variable for :collection in form for rails?

Here is a simple_form code in _form.html.erb.
<%= f.input :start_time, :label => "Start Timeļ¼š", :collection => #time_slot %>
#time_slot is a variable defined in the controller. It is an array and looks like:
#time_slot = ['00:30 AM','01:00 AM','01:30 AM','02:00 AM','02:30 AM','03:00 AM' ,'03:30 AM','04:00 AM','04:30 AM']
The problem is that the rendered view does not have the dropdown menu with the predefined time slots listed. Instead it only shows a text box.
How to show the drop down time slots instead of a text box? Thanks.
Firstly: input was deprecated in v2.3.8, so if you're using Rails3 you should probably not be using input. Instead, use the proper form helpers, for a drop-down box (or select tag) you'd want:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
but if you must - you can probably pass the input-type eg :type => :select but I've never tried that and it may not work.

observ_field with collection_select on rails 2.3.8?

I'm developing project on rails 2.3.8 and I need to observe field on drop down menu which develop using collection select. Please can some one explain how to observe field ?
My collection select code is like this
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => true}) %>
And I don't know how to use observe field for this. So please can some one explain about this ?
Related: Auto populate a text field based on another text field
observe_field(field_id, options = {})
Observes the field with the DOM ID specified by field_id and calls a callback when its contents have changed. The default callback is an Ajax call. By default the value of the observed field is sent as a parameter with the Ajax call.
Read this details: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/observe_field

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

Resources