草庐IT

filter_parameters

全部标签

ruby-on-rails - rails 是如何实现 before_filter 的?

我对Rails如何实现像before_filter这样的过滤器很感兴趣。但是看了源码还是一头雾水。我注意到rails的框架维护了一个filter_chain,并在目标方法之前运行过滤器。但是,我不明白一个重要的过程:rails是如何捕获方法调用的?我的意思是,例如,我有一个类Dog,并为方法bark设置了一个before_filter。当我调用dog.bark时,rails应该以某种方式捕获此调用,并改为运行其修改后的方法。但是,我在源代码中没有找到这样的代码。任何人都可以告诉我这个想法或指出代码所在的位置吗? 最佳答案 当您设置b

ruby - RSpec stub : Return the parameter

虽然我的问题很简单,但我没能在这里找到答案:如何stub方法并返回参数本身(例如在执行数组操作的方法上)?像这样:interface.stub!(:get_trace).with().and_return() 最佳答案 注意:stub方法已被弃用。请看thisanswer以现代方式做到这一点。stub!可以接受一个block。block接收参数;block的返回值是stub的返回值:classInterfaceenddescribeInterfacedoit"shouldhaveastubthatreturnsitsargument

ruby - 轨道 3/ ruby : ActiveRecord Find method IN condition Array to Parameters single quote issue

我在微博模型上创建了一个方法,它接受一个user_id数组。在此方法中,我使用以下“查找”方法来提取数组中所有用户的所有帖子。find(:all,:conditions=>["user_idIN(?)",args.join(',')])但是当ActiveRecord为这个查询生成SQL时,它用单引号将逗号分隔的ID列表括起来。这会导致查询只提取单引号内第一个数字的帖子,而不是所有数字。SELECT`microposts`.*FROM`microposts`WHERE(user_idIN('3,4,5'))ORDERBYmicroposts.created_atDESC查询应该看起来像这

ruby-on-rails - Rails before_filter 用于 Controller 中的特定操作

defnewbefore_filterdoredirect_to"/"unlesscurrent_admin||current_companyflash[:notice]='Youdonthaveenoughpermissionstobehere'unlesscurrent_admin||current_companyendCODECODECODEenddefeditbefore_filterdoredirect_to"/"unlesscurrent_admin.id=5flash[:notice]='Youdonthaveenoughpermissionstobehere'unles

ruby-on-rails - Rails 4.0 Strong Parameters 嵌套属性带有指向散列的键

我在玩Rails4.xbeta并试图让嵌套属性与carrierwave一起工作。不确定我在做什么是正确的方向。四处搜索之后,最终查看了rails源代码和强大的参数,我发现了以下注释。#Notethatifyouuse+permit+inakeythatpointstoahash,#itwon'tallowallthehash.Youalsoneedtospecifywhich#attributesinsidethehashshouldbewhitelisted.https://github.com/rails/rails/blob/master/actionpack/lib/actio

ruby-on-rails - rails : URL/path with parameters

我想生成一个URL作为/swimming/students/get_times/2013-01-01/2013-02-02从这条路线get_class_swimming_studentsGET/swimming/students/get_times/:start_date/:end_date(.:format)swimming/students#get_times如何将参数传递给get_class_swimming_students_path? 最佳答案 get_class_swimming_students_path('2013-

ruby-on-rails - 如何过滤rails中的参数?

Rails内置了日志过滤功能,因此您无需记录密码和信用卡信息。非常适用于此,但当您想要触发自定义日志(如电子邮件)并发送您自己的参数或其他数据时,参数显然不会自动过滤。我一直在挖掘并试图在Rails源代码中找到它,但到目前为止还没有运气。我已将rails配置为按如下方式过滤参数,它可以正常工作以将数据排除在rails日志之外:config.filter_parameters+=[:password,:password_confirmation,:credit_card]在将敏感数据转储到电子邮件、api调用或自定义(非Rails)日志之前,您如何从params哈希中过滤敏感数据?

ruby-on-rails - 带参数的 before_filter

我有一个方法可以做这样的事情:before_filter:authenticate_rights,:only=>[:show]defauthenticate_rightsproject=Project.find(params[:id])redirect_tosignin_pathunlessproject.hiddenend我还想在其他一些Controller中使用这个方法,所以我将这个方法复制到一个包含在application_controller中的helper。问题是,在某些Controller中,项目的id不是:id符号,而是f.e.:project_id(还有一个:id存在(

ruby-on-rails - 更改 :id parameter in Routing resources for Rails 的名称

我四处查看如何更改动态参数槽,发现这篇文章完全符合要求。帖子是https://thoughtbot.com/blog/rails-patch-change-the-name-of-the-id-parameter-in基本上它所做的是,如果以下是路线:map.resources:clients,:key=>:client_namedo|client|client.resources:sites,:key=>:namedo|site|site.resources:articles,:key=>:titleendend这些路由创建以下路径:/clients/:client_name/cli

ruby-on-rails - Rails 4 : before_filter vs. before_action

在rails>4.0.0中,生成器使用before_action而不是before_filter创建CRUD操作。它似乎做同样的事情。那么这两者有什么区别呢? 最佳答案 尽我们所能see在ActionController::Base中,before_action只是一个newsyntax对于before_filter。但是before_filter语法isdeprecated在Rails5.0中并将在Rails5.1中删除 关于ruby-on-rails-Rails4:before_fi