[英]Rails 4 with has_scope: fails silently
I have a Rails 4.0.0 app and want to include the has_scope gem. 我有一个Rails 4.0.0应用程序,想包含has_scope gem。
Very simple and straight resources: 非常简单直接的资源:
Model: 模型:
class Thing < ActiveRecord::Base
resourcify
...
belongs_to :client
validates_presence_of :name
scope :by_client, -> client { where(:client => client)}
scope :by_name, -> name { where(:name => name)}
Controller: 控制器:
class ThingsController < ApplicationController
include Pundit
before_filter :authenticate_user!
before_action :set_thing, only: [:show, :edit, :update, :destroy]
after_action :verify_authorized, :except => [:index, :edit]
has_scope :by_client, :type => :integer
has_scope :by_name
....
def index
@things = apply_scopes(Thing).all
puts current_scopes
end
current_scopes is always an empty hash and Thing.all is passed to the view, no matter what scope is applied. current_scopes始终是一个空哈希,无论应用什么范围,Thing.all都将传递给视图。 The scopes themself are ok and properly return the filtered records Parameters are there: eg:
范围本身就可以,并且可以正确返回过滤后的记录。参数存在:例如:
Started GET "/things?client=113" for 127.0.0.1 at 2014-07-26 16:07:32 +0200
Processing by ThingsController#index as HTML
Parameters: {"client"=>"113"}
What is wrong with this setup. 此设置有什么问题。 I got no warnings, no method missing etc. Just the full list of things.
我没有警告,没有方法遗失等等。只是全部的清单。 Did i miss some configuration option or anything?
我错过了一些配置选项或其他东西吗?
Got it, in this case, scope works on table colums. 知道了,在这种情况下,作用域适用于表列。 This fixes it:
可以解决此问题:
scope :by_client, -> client { where(:client_id => client)}
and than a 比一个
has_scope :by_client
and parameters 和参数
{"by_client"=>"113"}
are enough 够了
Based on the brief read I've had through the has_scope
documentation, the parameters has you're supplying is wrong and needs to be changed to 根据我对
has_scope
文档的简要阅读,您提供的参数是错误的,需要更改为
{ "by_client" => { "client" => "113" } }
And your controller needs to change the has_scope
to 并且您的控制器需要将
has_scope
更改为
has_scope :by_client, :using => :client, :type => :integer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.