未定义的方法字符串化!使用meta_search和active_admin宝石时出错 [英] Undefined Method stringify! error when using meta_search and active_admin gems
问题描述
我正在使用active_admin gem,因为它需要meta_search gem,所以我想在管理页面之外提供搜索功能。当我向Model.search方法提供字符串时,出现了未定义的方法错误。根据meta_search文档,这就是我要做的,所有active_admin搜索都可以完美地进行。我是否缺少某些东西?
I am using the active_admin gem and, since it requires the meta_search gem, I want to provide search functionality outside of the admin pages. I am getting an undefined method error when I provide a string to the Model.search method. According to the meta_search docs, this is all I need to do and all of the active_admin searching works flawlessly. Am I missing something?
Gemfile:
gem 'activeadmin'
gem "meta_search", '>= 1.1.0.pre'
控制器:
@eventsearch = Event.search(params[:q])
错误消息(example.com/?q=foo):
Error message ( example.com/?q=foo ):
undefined method `stringify_keys!' for "foo":String
推荐答案
Event#search
方法需要一个哈希,在哈希中,它们的键告诉它要在哪些字段中搜索(以及如何搜索)关联的值。但是在您的情况下, params [:q]
仅包含一个字符串( foo
)。
The Event#search
method expects a Hash where they keys tell it which fields to search (and how) for the associated values. But in your case params[:q]
just contains a string ("foo"
).
[meta_search的自述文件]中的入门部分将向您显示所需的参数类型。例如,如果您要搜索事件的 title
字段,则应执行以下操作:
The Getting Started section in [meta_search's README] will show you what kind of parameters it expects. If, for example, you wanted to search Events' title
fields you would do something like this:
# Given a URL like this: http://example.com/?title_contains=foo
# `params` will look like this: { :title_contains => "foo" }
# so we give `params` directly to the search method:
@events = Event.search params
这篇关于未定义的方法字符串化!使用meta_search和active_admin宝石时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!