【问题标题】:Rails render search bar sunspot searchRails 渲染搜索栏 太阳黑子搜索
【发布时间】:2014-04-26 01:09:10
【问题描述】:

我正在为同一个视图使用 to 方法并使用渲染,一切正常,但搜索仍然基于索引方法而不是我的其他方法。我是否必须为其他方法创建一个完整的新视图才能单独工作?

示例:

在我的控制器中,我有:

 def index

  @search = Post.search do #Post.search do
  fulltext params[:search]
  paginate :page => params[:page], :per_page => 20 
  order_by(:updated_at, :desc)
   end
   @posts = @search.results 
 end



 def local

  @search = Post.search do #Post.search do
  fulltext params[:search]
  with(:school, current_user.school)
  facet(:school)
  paginate :page => params[:page], :per_page => 20 
  order_by(:updated_at, :desc)
  end
   @posts = @search.results

   render :index  
end

如果我翻转方法并将索引设为本地,反之亦然,那么当我搜索时,搜索会根据 :school 过滤结果,但是代码的方式是,我有一个指向本地操作的链接,并且所有内容都被过滤了但是当我搜索时,无论学校领域如何,我都会得到结果

必须有比复制我的 index.html.erb 并将其更改为 local.html.erb 更好的解决方案

谢谢!

【问题讨论】:

  • 好的,我想我可以在视图中有一个if语句并传递不同的参数,所以我可以将参数搜索重命名为本地方法中的本地搜索

标签: ruby-on-rails search ruby-on-rails-4 render sunspot


【解决方案1】:

解决方案:

好的,我将本地方法的参数重命名为本地搜索,如下所示:

def local

  @search = Post.search do #Post.search do
  fulltext params[:localsearch]                   #changed from 'search' to localsearch'
  with(:school, current_user.school)
  facet(:school)
  paginate :page => params[:page], :per_page => 20 
  order_by(:updated_at, :desc)
 end
 @posts = @search.results

 render :index  
end

然后在视图 application.html.erb 我添加了这样的 if 语句:

    <% if current_page?(posts_path) %>
      <div class="pull-left" style="display: inline-block;"> <div role="search">      
      <%= form_tag posts_path, :method => :get do %>
      <%= text_field_tag :search, params[:search], id: 'search' , autofocus: false ,:placeholder => "search",:class =>'fruute-input', :size => 30 %>
        </div>
        <button type="submit" id="mybutton" >
          <i class="fa fa-search fa-lg" style = "color: white;"></i>                     
        </button>
        <% end %>
        <% end %>
        
     <% if current_page?(index_path) %>
      <div class="pull-left" style="display: inline-block;"> <div role="search">      
      <%= form_tag index_path, :method => :get do %>
        #in this next line I pass :localsearch to local method;
      <%= text_field_tag :search, params[:localsearch], id: 'search' , autofocus: false ,:placeholder => "search",:class =>'fruute-input', :size => 30 %>  
        </div>
        <button type="submit" id="mybutton" >
          <i class="fa fa-search fa-lg" style = "color: white;"></i>                     
        </button>
        <% end %>
        <% end %>

对于那些希望将来与太阳黑子一起使用的人:

在模型中:

  validates :price, :numericality => {:only_integer => true}
  searchable do
    text :title
    text :author 
    text :course
    text :school
    text :email
    text :description
    text :price
    integer :id
    time :updated_at


string :school #notice, facet fields must be string, just add the field from above and diplicate it as string if text

结束

砰!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 2015-12-20
    • 2014-03-29
    • 1970-01-01
    • 2014-09-19
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多