简体   繁体   English

Ruby on Rails:Ransack未定义的方法`result`

[英]Ruby on Rails: Ransack undefined method `result`

I'm new to ROR, so this could be a very simple problem. 我是ROR的新手,所以这可能是一个非常简单的问题。

I have just installed the ransack gem for my web application. 我刚为我的Web应用程序安装了ransack gem。 I am wanting to search on project names and clients in my database. 我想在我的数据库中搜索项目名称和客户端。 I have an index page view, which I use for my homepage, then a search page, which uses a search action that I created. 我有一个索引页面视图,我用于我的主页,然后是一个搜索页面,它使用我创建的搜索操作。

def index
@projects = Project.all

  respond_to do |format|
  format.html # index.html.erb
  format.json { render :json => @projects }
  end
 end


def search

  @q = Project.search(params[:q])
  @project_search = @q.result(:distinct => true)

  respond_to do |format|
    format.html # search.html.erb
    format.json { render :json => @projects }
  end
end

Here is part of my search view: 这是我的搜索视图的一部分:

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

When I try and load the search page, I get this error: 当我尝试加载搜索页面时,我收到此错误:

undefined method `search' 未定义的方法`搜索'

With the extracted source being in my project controller at this line 将提取的源位于此行的项目控制器中

@q = Project.search(params[:q])

Hopefully it's an easy fix, and you can explain what I'm doing wrong so I learn. 希望这是一个简单的修复,你可以解释我做错了所以我学习。

Any help at all will be appreciated. 任何帮助都将不胜感激。

Thanks in advance! 提前致谢!

EDIT: I've added my index action above to show you that I have an index action, and a search action. 编辑:我在上面添加了我的索引操作,以向您显示我有索引操作和搜索操作。 Here is my routes.rb file as well. 这是我的routes.rb文件。

FinalApp::Application.routes.draw do
resources :projects
match "search" => "projects#search", :as => :search
root :to => 'projects#index'
end

SECOND EDIT: 第二次编辑:

I hadn't restarted my server. 我没有重新启动我的服务器。 I now have another error. 我现在有另一个错误。

undefined method `result' 未定义的方法`结果'

With the extracted source being in my project controller at this line 将提取的源位于此行的项目控制器中

@project_search = @q.result(:distinct => true)

THIRD EDIT: 第三次编辑:

Another error :/ 另一个错误:/

undefined method `schema_cache' 未定义的方法`schema_cache'

With the extracted source being in my project controller at this line 将提取的源位于此行的项目控制器中

@q = Project.search(params[:q])

And you are sure you ran Bundle Install after adding the Gem to your Gemfile and restarted instance or have correct development env settings? 并且您确定在将Gem添加到Gemfile并重新启动实例或具有正确的开发环境设置后运行了Bundle Install?

Ransack adds the search method so it would seem it is not properly installed. Ransack添加了搜索方法,因此它似乎没有正确安装。

Problem solved in another question: Ruby on Rails: Advanced search 另一个问题解决了问题: Ruby on Rails:高级搜索

I have never used the gem ransack , but I think your first line def search should be def index and than you define the search method somewhere else... or have you included a route for search in your routes.rb? 我从来没有使用过的宝石ransack ,但我觉得你的第一线def search应该是def index比您定义的search method别的地方...或者你有包括用于路线search你的routes.rb?

Anyway, what you are looking for is a very simple search and I don't think you need any gem for it. 无论如何,你正在寻找的是一个非常简单的搜索,我认为你不需要任何宝石。

You can define the search method in your model: 您可以在模型中定义搜索方法:

product.rb product.rb

def self.search(search)
  if search
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

products_controller.rb: products_controller.rb:

def index
  @projects = Project.search(params[:search])
end

The search form can be included anywhere in your application: 搜索表单可以包含在您的应用程序的任何位置:

<% form_tag projects_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

Please take a look here and if you need an advanced search look here . 请看这里 ,如果您需要高级搜索,请查看此处

I hope it helps 我希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM