Undefined Method stringify! error when using meta_search and active_admin gems

时间秒杀一切 提交于 2019-12-02 15:42:59

问题


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'

Controller:

  @eventsearch = Event.search(params[:q])

Error message ( example.com/?q=foo ):

  undefined method `stringify_keys!' for "foo":String

回答1:


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").

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


来源:https://stackoverflow.com/questions/9237629/undefined-method-stringify-error-when-using-meta-search-and-active-admin-gems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!