在基于pg_search查询字符串的Rails 5控制器中设置布局

时间:2021-02-02 23:09:25

I am using pg_search and would like to conditionally set a layout template based on a query string. Below is my index method which I know is wrong, but wanted to provide an idea of what I am trying to do.

我正在使用pg_search,并希望根据查询字符串有条件地设置布局模板。下面是我的索引方法,我知道这是错误的,但想知道我想要做什么。

products_controller.rb

products_controller.rb

...
def index
  if params[:query]
     if :query.include?('limestone')
        layout 'product-index-limestone'
      end
     @products = Product.search_for(params[:query])
  else
  ...
  end
end
...

1 个解决方案

#1


1  

You need to write respond_to block in your controller action like below way and set layout within it

您需要在控制器操作中编写respond_to块,如下所示,并在其中设置布局

def index 
    respond_to do |format| 
      if params[:query].include?("limestone")
         format.html {render :index, layout:'limestone'}
      elsif params[:query].include?("gray") format.html 
        {render :index, layout:'gray'} 
      end # end of if
    end #end of respond_to block
end

#1


1  

You need to write respond_to block in your controller action like below way and set layout within it

您需要在控制器操作中编写respond_to块,如下所示,并在其中设置布局

def index 
    respond_to do |format| 
      if params[:query].include?("limestone")
         format.html {render :index, layout:'limestone'}
      elsif params[:query].include?("gray") format.html 
        {render :index, layout:'gray'} 
      end # end of if
    end #end of respond_to block
end