I am trying to get the following to work - Basically, the button submits to the searches_controller index action and SHOULD pass the params[:search_task] to it. But for some reason its not working.
<div class="btn-group InterestGroup" role="group" aria-label="">
<button class = "btn btn-success InterestStatButton"><%= User.tagged_with(interest, :on => :tags, :any => true).count %></button>
<%= button_to interest, searches_path, :method => :get, :class => "btn btn-default InterestLabelButton" %>
<%= hidden_field_tag :search_task, interest, :id => "search", :class => "form-control" %>
</div>
On the very same page in the header I have this in the header which is an input field doing the same thing, and works fine. What I don't get it if you look at what each is producing in the HTML the hidden field in the first block of code is the same as the input in the form_tag in the second block of code.
<%= form_tag searches_path, html: {class: "navbar-form navbar-left"}, :method => :get do %>
<div class="form-group" style="display:inline;">
<div class="input-group" style="display:table; width:350px;">
<span class="input-group-addon" style="width:1%;"><span class="glyphicon glyphicon-search"></span></span>
<%= text_field_tag :search_task, nil, class: "form-control", id: "search", placeholder: "Search for members or content", label: false %>
</div>
</div>
<% end %>
The problem is that button_to
is a self-contained method (IE you cannot pass a block etc):
Generates a form containing a single button that submits to the URL created by the set of options.
When you use:
<%= button_to interest, searches_path, :method => :get, :class => "btn btn-default InterestLabelButton" %>
<%= hidden_field_tag :search_task, interest, :id => "search", :class => "form-control" %>
... it simply won't be added to the form, hence will not be passed.
How to add additional params to a button_to form?
What you'll need is to add the search_task
param to your button_to
helper:
<%= button_to interest, searches_path, method: :get, class: "btn btn-default InterestLabelButton", params: { search_task: interest } %>
The button_to
form sends a POST
request by default. This will mask the passed params; if you want to use GET
, you've done the right thing and declared it. An important note is that the GET
request appends params to the request URL.
You can read more about it here: http://www.w3schools.com/tags/ref_httpmethods.asp