Rails redirect with collection_select and javascript -
i have collection_select fires js set id in url when click occurs. here code.
application.js
$(document).ready(function(){ $("#org_id").on("change", function(){ val = $(this).val(); window.location = '/sessions?org_id='+ val; }); });
view
<div id="org-select"> <%= collection_select :org, :id, org.all, :id, :name %> </div>
rendered html
<div id="org-select"> <select id="org_id" name="org[id]"><option value="1">bustas</option> <option value="2">wintas</option></select>
what of give me url /sessions?org_id=2.
the issue having select box in page defaults first org, , when user changes option in select, page fires/refreshes, page defaults first org, id in url not change.
well, collection_select
have option set default value. can see on last example provided user here:
http://apidock.com/rails/actionview/helpers/formoptionshelper/collection_select
what can set selected value params getting url, this:
<%= collection_select :org, :id, org.all, :id, :name, {:selected => params[:org_id]} %>
the ?org_id=2
part of url translated params[:org_id]
internally, can use example above.
give try , see if works.
Comments
Post a Comment