首页 > 解决方案 > 如何将我的复选框过滤器与我的搜索表单相关联?

问题描述

我有一个搜索帖子的搜索表单,但我希望用户能够根据各种属性过滤这些搜索。如何将复选框过滤器链接到搜索?我正在使用太阳黑子宝石过滤帖子。

这是post.rb

class Post < ActiveRecord::Base
    belongs_to :user


    searchable do
        text :details
        text :title
    end


end

这是帖子文件夹中的 show.html.erb


<div class = "container">
  <% @posts.each do |x| %>
    <div class="fb" id="download">
      <div class="post">
           <div class="top">
             <div class="name">
               <strong><a href="#"><span class="text-name"><%= x.title %></span></a></strong>
              </div>
            </div>
            <span class="text-message"><%= x.details %></span><br><br>
        </div>
    </div>
  <% end %>
</div>

这是我的帖子控制器中的显示功能

def show 
    @search = Post.search do
      fulltext params[:search]
    end
    @posts = @search.results
  end

这是我的 application.html.erb 中的搜索表单

<% if current_user.profile %>
                    <li><%= form_tag user_post_path(user_id: current_user.id), :method => :get do %>
                    <p>
                    <%= text_field_tag :search, params[:search], autocomplete: "off", placeholder: "Try \"Tennis\"" %>
                    <button type="submit"><i class="fa fa-search"></i>
                    <% end %></li>
                    <li><%= link_to "Create Post", new_user_post_path(user_id: current_user.id), class: "linkfocus"%></li>
                    <li><%= link_to "Profile", user_path(id:current_user.id), class: "linkfocus"%></li>
<% else %>

标签: ruby-on-rails

解决方案


推荐阅读