【问题标题】:Ransack : Undefined Method "result"Ransack:未定义的方法“结果”
【发布时间】:2013-10-07 14:07:27
【问题描述】:

我需要使用 Ransack,我安装了 gem,我重新启动了我的服务器,我在我的控制器中写了这个:

定义索引

  @q = Sound.search(params[:q])
      @sounds = @q.result

结束

在我看来:

    <%= f.label :title_cont %>
    <%= f.text_field :title_cont %>


    <%= f.submit "Search" %>

但我有这个错误:

#ActiveRecord::Relation:0x007fa185e9c2c0 的未定义方法“结果”>

我试图找到答案,但一切都不起作用。

感谢您的帮助

编辑: 我找到了我的解决方案,我使用了另一种宝石:Sunspot 和那个工作。

感谢大家的帮助。

【问题讨论】:

  • Sound.search(params[:q]) 已经返回结果。你应该使用@sounds = Sound.search(params[:q])
  • @MrYoshiji 这与readme for Ransack 相矛盾,如果是这样的话,这会是一个更常见的问题吗?
  • @MrYoshiji ,我使用 Readme for Ransack 来使用 gem。我不明白你的答案,你能解释更多吗?或者如果可能的话做一个演示?谢谢
  • 我的错,这是一个错误的评论,我在这里帮不上忙,我误解了你的问题
  • 您确定您的声音模型还没有定义“搜索”方法吗?如果你在 Rails 控制台中输入 Sound.search,你会得到 Ransack::Search 的对象类型吗?

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 ransack


【解决方案1】:

我也遇到了同样的问题。

我在这里找到了解决方案 https://github.com/activerecord-hackery/ransack/issues/266

我变了

@q = Sound.search(params[:q])
@sounds = @q.result

到

@q = Sound.ransack(params[:q])
@sounds = @q.result

我认为 ActiveAdmin 已经覆盖了我的搜索方法,因此,如上面链接中所述,已创建 ransack 方法来运行搜索,以防另一个 gem 使用了“搜索”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2013-04-24
    • 2015-11-15
    • 1970-01-01
    相关资源
    最近更新 更多