标头部分中的ransack搜索表单:没有将Ransack :: Search对象提供给search_form_for [英] ransack search form in header partial: No Ransack::Search object was provided to search_form_for

查看:186
本文介绍了标头部分中的ransack搜索表单:没有将Ransack :: Search对象提供给search_form_for的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是RoR的新手,所以答案可能很明显,在这种情况下,我深表歉意.我环顾四周,没有发现任何有用的东西.

First of all, I'm new to RoR, so the answer may be obvious, in which case I apologize. I've looked around and haven't found anything that helps.

我正在尝试在我的应用程序的每个网页的标题处都有一个搜索表单,该表单将搜索我所有存储桶"的名称.这是相关代码:

I'm trying to have a search form at the header of every web page on my app that will search through the names of all my "buckets". Here is the relevant code:

在app/views/layouts/_header.html.erb中(在导航栏中):

In app/views/layouts/_header.html.erb (within a nav bar):

<% search_form_for @q do |f| %>
  <%= f.label :name_cont %>
  <%= f.text_field :name_cont %>
  <%= f.submit %>
<% end %>

在app/controllers/buckets_controller.rb中:

In app/controllers/buckets_controller.rb:

def index
  unless params[:q].blank?
    @q = Bucket.search(params[:q])
    @buckets = @q.result.paginate(:page => params[:page])
  else
    @buckets = Bucket.find(:all, :limit => 5).paginate(:page => params[:page])
  end
end

我知道最后一部分并不是那么好:我要尝试做的是,如果我只是访问存储桶索引页面(而不是通过搜索),则显示5个最近创建的存储桶.当我以标题形式搜索内容时,我访问索引页面,但仅显示命中搜索的存储桶. (将搜索页与索引页分开的更好的处理方法吗?)

I understand the last part isn't that great: what I'm trying to do is if I'm just accessing the bucket index page (not by searching), i display the 5 most recently created buckets. When I search for something in the header form, I access the index page but only show the buckets that hit the search. (would a better way to handle it to have a search page separate from my index page?)

我发现了此问题,它几乎是相同的,但我仍然没有看看如果每个页面上都有表格,我将如何处理@q -当然,我不必更改每个控制器的每个动作吗?

I found this issue which is pretty much identical, but I still don't see how I handle @q if every page is going to have the form on it--surely I don't have to alter every controller's every action?

对不起,请您谅解我的肥胖!

Sorry in advance for any frustration my noobishness my cause you!

推荐答案

正如其他人所说,您需要利用ApplicationController的before_filter.尽管ernie本人似乎不建议这样做,但实现起来很简单.

As others have said, you need to utilize the ApplicationController's before_filter. Though ernie himself seems not to recommend this, the implementation is simple.

首先,使用高级Ransack选项设置搜索路径

First, use the advanced Ransack options to set your path for your search thusly

#config/routes.rb
  resources :buckets do
    collection do
      match 'search' => 'buckets#search', via: [:get, :post], as: :search
    end
  end

第二,更新您的BucketsController,使其包含以下自定义操作:

Second, update your BucketsController to include the following custom action:

#controllers/buckets_controller.rb
  def search
    index
    render :index
  end

没有什么不寻常的.如果您当前尝试搜索,则会从原始问题中得到错误信息.您对变量q的定义已正确实现,但必须将其移至ApplicationController,如下所示:

Nothing yet out of the ordinary. If you currently try to search you will get the error from your original question. Your definition of the variable q is correctly implemented, but you will have to move it to the ApplicationController like so:

#controllers/application_controller.rb
before_filter :set_global_search_variable

  def set_global_search_variable
    @q = Bucket.search(params[:q])
  end

最后,更新您的搜索表单以传递正确的搜索选项

Finally, update your search form to pass in the correct search options

#layouts/_header.html.erb

    <% search_form_for @q, url: search_buckets_path, html: { method: :post }  do |f| %>
      <%= f.label :name_cont %>
      <%= f.text_field :name_cont %>
      <%= f.submit %>
    <% end %>

这篇关于标头部分中的ransack搜索表单:没有将Ransack :: Search对象提供给search_form_for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆