How to use form_tag to update params

Posted by Tryskele on Stack Overflow See other posts from Stack Overflow or by Tryskele
Published on 2010-04-28T16:08:25Z Indexed on 2012/09/25 9:37 UTC
Read the original article Hit count: 203

Filed under:

I have been struggling with a problem in Rails for a couple of days and still could not find the solution. Could you help me with that?

Problem: I have a search box that puts a :search_string entry in the params structure. I use a form_tag for that and it works fine.

<% form_tag :controller=> 'items', :action => 'find' do %>
    <%= text_field_tag :search_string, params[:search_string] %>
<% end %>

The problem is when I want to add and update other params key-value (in another view), for instance :start_date, to filter the search_string result. Here is the code snipped that I use in the view:

<% form_tag :controller=> "items", :action => "find", :params => params do %>
    <%= hidden_field_tag  :date_start,  '2010-04-01' %>
    <%= submit_tag 'April' %>
<% end %>

<% form_tag :controller=> "items", :action => "find", :params => params do %>
    <%= hidden_field_tag  :date_start,  '2010-03-01' %>
    <%= submit_tag 'March' %>
<% end %>

When I first click on "April" submit button, then the params is correctly passed to the controller (i.e. there is a params[:start_date]='April'). However when I try to click "March" button afterwards, the params[:start_date] is not updated. I definitely think this is a stupid newbie mistake, but I cannot figure out how to properly use the form_tag. Could you tell me if I am doing something work? Otherwise, could you advise me which is the best way to update the params using form_tag's ? Thank you very much in advance.

Miquel

© Stack Overflow or respective owner

Related posts about ruby-on-rails