带有 has_scope 的 Rails 4:静默失败 [英] Rails 4 with has_scope: fails silently

查看:42
本文介绍了带有 has_scope 的 Rails 4:静默失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 4.0.0 应用,想要包含 has_scope gem.

I have a Rails 4.0.0 app and want to include the has_scope gem.

非常简单直接的资源:

型号:

  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)}

控制器:

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 总是一个空的哈希值,并且 Thing.all 被传递给视图,无论应用什么范围.范围本身没问题,并正确返回过滤后的记录参数在那里:例如:

current_scopes is always an empty hash and Thing.all is passed to the view, no matter what scope is applied. 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)} 

而不是一个

has_scope :by_client

和参数

{"by_client"=>"113"}

够了

推荐答案

根据我对 has_scope 文档的简要阅读,您提供的参数是错误的,需要改为

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

{ "by_client" => { "client" => "113" } }

并且您的控制器需要将 has_scope 更改为

And your controller needs to change the has_scope to

has_scope :by_client, :using => :client, :type => :integer

这篇关于带有 has_scope 的 Rails 4:静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆