草庐IT

request_method

全部标签

ruby - rake 任务有 "method_missing"吗?

如果我的Rakefile没有找到具有特定名称的任务,我希望rake根据特定规则使用该名称创建一个新任务,如果一个文件缺少任务名称存在。但如果没有,我想回退到默认值(“不知道如何构建任务'foo'!”)。简而言之,Rake是否有method_missing? 最佳答案 我没试过,但快速搜索显示this.Ifyoudefinearulewithanemptystring,youcancatchanytaskthathasn’tbeendefinedelsewhere.Thismakesiteasytodynamicallycreater

ruby-on-rails - rails : dynamically define class method based on parent class name within module/concern

我想根据包含此Mixin的类名在Mixin中动态生成一个类方法。这是我当前的代码:moduleMyModuleextendActiveSupport::Concern#defsome_methods#...#endmoduleClassMethods#HereiswhereI'mstuck...define_method"#{self.name.downcase}_status"do#dosomething...endendendclassMyClass但这给了我以下方法名称:MyClass.mymodule::classmethods_status在方法定义中获取基类名称是可行的(s

子类中的 Ruby : Invoke overridden method of parent class,

假设我有从A派生的B类是否可以像这样调用A的重写方法?classAdefmethod1enddefmethod2endendclassB#与HowdoIcallanoverriddenparentclassmethodfromachildclass?不完全相同,但我们似乎想做同样的事情。 最佳答案 我在这里假设B应该继承自A,而您只是在示例代码中输入了一个错字。如果不是这样,就没有办法做你想做的事情。否则,您可以通过将A的method2实例方法绑定(bind)到您当前的B对象并调用它来使用反射来做您想做的事这个:classAdefm

ruby-on-rails - 动态类加载 : Is there a "method_missing" for classes in Ruby?

在我正在处理的Rails应用程序中,我有一个与单表继承模型“Node”相关的模型“Type”:Node的任何可能的子类都被定义为类型表中的一个Type。现在可以在初始化程序中加载所有类,但我想仅在需要时加载子类。我能想到的最佳解决方案是回退到一个未初始化的常量上,该常量将检查该常量是否可以代表应用程序中的一个类,类似于method_missing所做的事情。我想要一些关于如何以及在何处定义此逻辑的建议,或者是否有更好的解决方案。 最佳答案 我不知道这是否是新的,但我认为它值得添加。可以将缺少的方法用作类方法classExampled

ruby-on-rails - 在 Ruby on Rails 中更改 request.remote_ip 的值

出于测试目的,我想更改request.remote_ip的返回值。在我的开发机器上,它总是返回127.0.0.1,但我想给自己不同的假IP来测试我的应用程序的正确行为,而不是先将它部署到实时服务器!谢谢。 最佳答案 如果您希望在整个应用程序中使用此功能,那么在您的app/helpers/application_helper.rb中覆盖remote_ip方法可能会更好/更容易:classActionDispatch::Request#rails2:ActionController::Requestdefremote_ip'1.2.3.

ruby - 是 &method( :method_name) idiom bad for performance in Ruby?

我最近遇到了&method(:method_name)语法。(这使用Object#method方法-RDoclink)例如,[5,7,8,1].each(&method(:puts))相当于[5,7,8,1].each{|number|putsnumber}在Ruby的各种实现中,与前者相比,后者是否存在性能损失?如果是,实现者是否正在努力提高其性能? 最佳答案 是的,这似乎对性能不利。deftimestart=Time.nowyield"%.6f"%(Time.now-start)enddefdo_nothing(arg)endR

ruby-on-rails - rails : Model instance method or helper method?

按照惯例,应将以下定义为我的模型的实例方法还是辅助方法?#app/models/user.rbclassUser或#app/helpers/users_helper.rbmoduleUsersHelperdeffull_name"#{@user.first_name}#{@user.last_name}"endend非常感谢。 最佳答案 选择第一个(保留在模型中),也是因为您可能想做其他事情,比如组合索引:) 关于ruby-on-rails-rails:Modelinstancemeth

ruby-on-rails - 为什么我不能用重载方法在 define_method 中调用 super?

当我运行下面的代码时会引发错误:implicitargumentpassingofsuperfrommethoddefinedbydefine_method()isnotsupported.Specifyallargumentsexplicitly.(RuntimeError).我不确定是什么问题。classResultdeftotal(*scores)percentage_calculation(*scores)endprivatedefpercentage_calculation(*scores)puts"Calculationfor#{scores.inspect}"scores

ruby-on-rails - rails : use jbuilder template in various controller methods

是否可以在另一个Controller方法中重用jbuilder-template?换句话说:如何明确地说Controller方法使用具体的jbuilder-template? 最佳答案 来自Railsguide.从另一个Controller渲染一个Action的模板。Whatifyouwanttorenderatemplatefromanentirelydifferentcontrollerfromtheonethatcontainstheactioncode?Youcanalsodothatwithrender,whichacce

ruby - RuntimeError: :json 未在 Faraday::Request 上注册

我是ruby​​的新手,我正在尝试执行规范,但收到此错误:RuntimeError:         :jsonisnotregisteredonFaraday::Request我正在尝试使用这个gem:https://github.com/Chicago/windyMacOSX10.7.4Ruby1.9.3,使用RVM 最佳答案 我刚刚在升级Faraday时遇到了这个问题。json请求中间件在0.8中从Faraday中移除。它现在位于faraday_middlewaregem中。您应该geminstallfaraday_middl