Q:

Collection_select redirecting with parameters with the help of javascript

I am trying to make a redirect the user to a route 'attendance/:domain' when the option from the dropdown that I made changes. I am doing this using javascript. Earlier implementations like: window.location = "<%= url_for(:controller => 'attendance', :action => 'index') %>?" + id; just add the id to the earlier link so if I had 'attendance/2' it would become 'attendance/23' instead of 'attendance/3' . So I am trying to use the path provided by ruby like so:  
config.rb:
get 'attendance/:domain', to: 'attendance#index', as: 'attendance_domain'

view:  
<div class="form-group col-lg-3 col-md-5 ">
        <%= label_tag(:domain, "Domeniu", class: "control-label") %>
        <%= collection_select(nil, :id, @domains, :id, :complete_name, {selected: -1}, {:onchange=> "redirectToIndex(this.value)",:selected => params[:domain],class: "form-control"}) %>
    </div>

javascript in view:
function redirectToIndex(trainerId){
     window.location ="<%= url_for(attendance_domain_path(" + trainerId + ")) %>";
  } 

In this implementation the link that I am redirected to is : http://localhost:3000/attendance/ + trainerId + which is not what I wanted. Instead of trainerId it should be the value of that variable.  Any tips please ?
In the end my question is : How can I redirect with parameters using collection_select. This dropdown will be available on the same page so the parameters need to change accordingly not like in the situation described above.

A:

Just pass the full url to redirectToIndex() from collection_select via proc. Try this in view:
<%= collection_select(nil, :id, @domains, -> (d) {attendance_domain_path d},
  :complete_name, {selected: -1}, {:onchange=> "redirectToIndex(this.value)",
  :selected => params[:domain],class: "form-control"}) %>

In JS:
function redirectToIndex(val){
 window.location = val;

}

