Ctpelnar1988 Ctpelnar1988 - 5 months ago 20
Ruby Question

Pass value of select_tag with link_to

Example:

How can I ask the user for their

gender
with a
select_tag
, and then pass the value through a link?

HTML.ERB

<%= select_tag "gender", "<option value='male'>Male</option><option value='female'>Female</option>".html_safe %>

<%= link_to "Gender Evaluation", gender_eval_path(info: {gender: params[:gender]} %>

Answer

You need to add on:change event listener to your select tag, and in according function replace href attribute for your link.

example:

assume your select tag has class gender-select and link submit-link

$(document).ready(function(){
    $('.gender-select').on('change', function(){
        $('.submit-link).attr('href', 'some_url/?gender'+ $('.gender-select').val();
    })
})

Replace some_url with real url