首先,我正在遵循此处针对 Rails 问题的做法(好主意!):https ://gist.github.com/1014971
我收到一个错误:
undefined method `search' for #<Class:0x5c25ea0>
app/controllers/accessories_controller.rb:6:in `index'
我确实在/config/application.rb中加载了我的 /app/models/concerns/ 目录。所以“关注”模块正在被加载。只是想指出这一点。
这是我的代码:
/app/models/concerns/searchable.rb
module Searchable
extend ActiveSupport::Concern
# Add a "search" scope to the models
def self.search (search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
/app/models/accessory.rb
class Accessory < ActiveRecord::Base
include Searchable
...
end
/app/controllers/accessories_controller.rb
class AccessoriesController < ApplicationController
def index
@accessories = Accessory.search(params[:search])
...
end
end