0

我有一个索引页面Users,我想使用 MetaSearch 使用搜索表单进行过滤。但是,单击复选框时要搜索的值存储为字符串。例如,这是我想将 MetaSearch 应用到的表单:

<% form_for(current_user.profile) do |f| %>
<table id="careerCriteria">
  <tr>
    <td class="normal"><%= current_user.profile.hometown %></td>
    <td><%= check_box_tag :hometown %></td>
  </tr>
  <tr>
    <td class="normal"><%= current_user.profile.current_city %></td>
    <td><%= check_box_tag :current_city %></td>
  </tr>
  <tr>
    <td class="normal"><%= current_user.profile.past_city %></td>
    <td><%= check_box_tag :past_city %></td>
  </tr>
</table>
<% end %>

我的用户模型:

class User < ActiveRecord::Base
  has_one :profile, :dependent => :destroy
end

我不想使用搜索按钮。我希望在单击复选框(或多个复选框)时应用过滤器。我是编程新手,所以任何帮助将不胜感激!

4

1 回答 1

1

你需要一点 ajax 和查询来完成这个。

这是一篇很好的文章,向您展示如何使复选框提交表单。

http://trevorturk.com/2010/08/24/easy-ajax-forms-with-rails-3-and-jquery/

您要做的是在控制器中创建一个操作来处​​理您的搜索。这是搜索操作的示例...

def search
    if params[:term].blank?
        raise "You must provide search criteria."
    end

    params[:term] = "%#{params[:term]}%"
    conditions    = " Description LIKE :term"

    @careers = Career.all(
        :conditions => [conditions, params],
        :offset     => params[:offset],
        :limit      => params[:limit]
    )

    respond_with @careers
end

您还需要为此操作设置此搜索的路线。

resources :careers do
    get "search/:term/:offset/:limit.:format", :action => "search", :constraints => { :offset => /\d+/, :limit => /\d+/ }
end

一旦您将表单提交到此操作,您应该能够使用 jQuery 来更新结果。

现在请记住,如果您不想使用 Ajax 和 jQuery 来加载结果,您可以这样做,您只需从表单标签中取出远程操作,它就会刷新整个页面。

于 2011-09-16T07:09:23.050 回答