开发者

Carrying along params in a form

开发者 https://www.devze.com 2023-01-20 10:28 出处:网络
Let\'s say I have a Students table on my main page, with Country, Name, Age columns. Suppose I want to have a dropdown box that le开发者_如何学Gots users filter students by country, and also a search

Let's say I have a Students table on my main page, with Country, Name, Age columns. Suppose I want to have a dropdown box that le开发者_如何学Gots users filter students by country, and also a search box that lets users search for students with a particular name.

I know how to create the country dropdown filter by itself:

<% form_tag('/students', :method => :get) do %>
  <%= collection_select(:country, :id, Country.all, :id, :name, :include_blank => true) %>
  <%= submit_tag 'Filter by Country' %>
<% end %>

And I know how to create the name search filter by itself:

<form name="sform" id="sform" action="" style="display:inline;">
  <label>
    Search names:
  </label>
  <%= text_field_tag("name", params['name'], :size => 10 ) %>
  <%= submit_tag("Go") %>
</form>

But what if I want to filter by country, and then search for names only within that country? I'm passing all my parameters around in the URL, but I don't know how to ensure that any existing params get passed along in the form.

[And in general, the forms I'm using above are kinda hacky, since I don't really understand Rails forms, so suggestions on improving them are also welcome.]


Seriously consider dumping all of the homegrown code for searchlogic. It handles everything you're asking for here, and greatly streamlines your view and controller code.

0

精彩评论

暂无评论...
验证码 换一张
取 消