lllllll
lllllll

Reputation: 4855

Passing the selected value from select_tag to params without having a form_tag

I have this code:

<div class="export-buttons">
    <div class="row">
        <div class="col-lg-offset-4">
            <%= label_tag :year, "Any" %>
            <%= select_tag :year,options_for_select((2014..2040).map{|x| x.to_s}),name:"params[year]" %>

        </div>
    </div>
    <div class="row">
        <div class="col-lg-4 col-lg-offset-4">
            <%= link_to "Revenue summary", revenue_summary_path(format:"pdf"),class:"btn btn-bg btn-default", role:"button"  %>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-4 col-lg-offset-4">
            <%= link_to "Fees summary", fees_summary_path(format:"pdf"),class:"btn btn-bg btn-default", role:"button"  %>
        </div>
    </div>
</div>

And I would like to pass the selected value in the select_tag to params. However I do not have a form_tag, hence there's no Submit button either.

Is there a way to "embed" it inside the params hash? Or do I need a form_tag for sure? In case of the latter, what would be the best approach, two submit-buttons + one path + if/else @controller?

Upvotes: 1

Views: 550

Answers (1)

Rahul garg
Rahul garg

Reputation: 9362

If you really want to avoid form tags, which may seems logical too since you want to have multiple download links which would simply send out an additional parameter of select_tag to the backend. You can use javascript for same.

  • Instead of link_to , you can use a binding to a javascript function onclick of these links.
  • The binded function would execute the url and before that it would append the current value of select filter via javascript to the url.

Upvotes: 1

Related Questions