草庐IT

sflt_filter_variable

全部标签

Ruby 元编程 : How can make module method see class's variable

例如我有一个模块和一个类:moduleSimpleModuledefself.class_helloputs"hellofrom#{@@name}"endendclassSimpleClass@@name='StackOverFlow'defself.testSimpleModule.class_helloendend然后我通过从类中调用模块方法进行测试:SimpleClass.test我遇到异常:uninitializedclassvariable@@nameinSimpleModule(NameError)我知道这里是因为模块范围与类范围不同。所以我的问题是:如何为SimpleMo

Ruby 元编程 : Initialize singleton_class variable

为什么Foo.val在调用Foo.set之前返回nil而不是"foo"?是否有任何机制可以在类评估时初始化@val?@val="foo"存储在哪个范围内?classFooclass 最佳答案 您可以像这样在Foo中初始化@val:classFoo@val="foo"class"foo"Foo.set("bar")pFoo.val#=>"bar"您的代码不是在Foo上而是在Foo的元类上初始化@val 关于Ruby元编程:Initializesingleton_classvariable,

ruby - 有没有 'variable_get'方法?如果没有,我该如何创建自己的?

由irb提供:2.0.0-p0:006>@instance_variable="fromaninstancevariable"=>"fromaninstancevariable"2.0.0-p0:007>variable="fromavariable"=>"fromavariable"2.0.0-p0:008>instance_variable_get(:@instance_variable)=>"fromaninstancevariable"2.0.0-p0:009>variable_get(:variable)NoMethodError:undefinedmethod`variab

javascript - rails 3.1 : The new way to use 'respond_to ... format.js' without the 'page' variable

我正在尝试将RubyonRails更新到3.1版本。我关注了UpgradingtoRails3.1截屏视频和所有内容似乎都有效,除了format.js{render(:update){|page|page.redirect_to@article}}在许多Controller中,我有如下代码:defcreate...respond_todo|format|format.js{render(:update){|page|page.redirect_to@article}}endend在上述所有情况下,当我尝试提交执行JS请求的相关表单时,出现以下错误:ActionView::Missing

ruby-on-rails - Ruby "don' t care variable"和Prolog的一样?

这个问题在这里已经有了答案:Whereandhowisthe_(underscore)variablespecified?(2个答案)关闭8年前。所以我对Prolog和Ruby都是新手。在大学学习Prolog,在我自己学习Ruby。我在想Ruby中是否像Prolog中那样存在“不关心”或“丢弃”变量。我刚刚打开irb并做了这个(假设下划线是“无关”的标志)1.9.2-p290:003>_,b,c=[1,2,3]=>[1,2,3]1.9.2-p290:004>b=>21.9.2-p290:005>c=>3结果果然如我所料。但是后来我很好奇下划线的值(value)在哪里以及它是什么类1.9

ruby - Rails:ParameterFilter::compiled_filter 尝试复制符号

我正在使用railsexception-notifiergem运行rails3。当发生异常并且应该发送电子邮件时,我从ParameterFilter类中得到一个异常。我在Rails源代码中发现了问题,但我不确定继续的最佳方式。问题出现在ActionDispatch::Http::ParameterFilter。在compiled_filter方法中,第38行报错:key=key.dupwhenkeyisasymbol,因为symbols是不可复制的。这是来源:defcompiled_filter...elsifblocks.present?key=key.dupvalue=value.

ruby-on-rails - ruby instance_variable_get 返回 nil

我的instance_variable_get方法有问题,因为它总是用我的对象实例之一返回nil对象。这是我的代码:logger.info"ASDF:"+@d_tree.inspectlogger.info"ASDF:"+@d_tree.instance_variable_get(:@content);输出是:ASDF:#TypeError(can'tconvertnilintoString):/app/controllers/d_trees_controller.rb:38:in`+'根据检查对象似乎没问题,但instance_variable_get返回一个nil对象感谢您的帮助!

ruby-on-rails - 使用 config.filter_parameters 在 rails 3 中自定义过滤参数

我正在努力从Rails2.3.11升级到3.0.10,但在转换ApplicationController的filter_parameter_logging中的内容时遇到问题。我想过滤这两个特定参数,如果它们出现在类似:referrer标签的值中,我也会过滤它们。我可以在我的application.rb中过滤掉常规参数config.filter_parameters+=[:password,:oauth,...]但我遇到的麻烦是我们还传入filter_parameter_logging的block。它还会过滤掉任何看起来像url的值中的参数,因此类似http://example.com?

ruby-on-rails - before_filter 与另一个 Controller

如果用户已登录,我正在尝试创建一个将检查每个页面的操作。为此,在ControllerHome中我创建了这个方法:defcheck_sessionif!session[:user_id]redirect_to:action=>'login'endend我把这段代码放在Controller的头部:before_filter:check_session,:except=>[:sub_layout,:authenticate,:login]现在我想在Home页面之外使用check_session,比如在Users页面中。在before_filter中调用不同Controller的方法的正确语法

ruby-on-rails - rspec 用户测试给出 "undefined local variable or method ` confirmed_at' "

我的rspec测试给了我NameError:undefinedlocalvariableormethod`confirmed_at'for#我的用户规范是:require'spec_helper'describeUserdobefore(:each)do@user=Factory(:user)end#Factorywillmakesurethatinthefutureifattributesareaddedthetestsbelowdon'tbreak#JustaslongastheFactoryisupdatedforthenewattributesasappropriate.con