简体   繁体   English

如何在Rails Controller / Action中使用Ruby改进?

[英]How do I use Ruby refinements inside Rails Controller/Action?

If I try outside the controller it works: 如果我尝试在控制器之外工作,则可以:

using ParamsExtension
class ApplicationController

If I try inside the controller or an action it does not work: 如果我尝试在控制器内部执行某项操作,或该操作无效:

class ApplicationController
using ParamsExtension

It throws 'undefined method `using'. 它抛出“未定义的方法”使用”。

I read this article and the author is using it inside a class: timelessrepo.com/refinements-in-ruby 我读了这篇文章,作者正在一个类中使用它: timelessrepo.com/refinements-in-ruby

I read the following from the documentation: 我从文档中阅读了以下内容:

You may only activate refinements at top-level, not inside any class, module or method scope. 您只能在顶层激活优化,而不能在任何类,模块或方法范围内激活。 You may activate refinements in a string passed to Kernel#eval that is evaluated at top-level. 您可以激活传递给Kernel#eval的字符串中的优化,该字符串在顶级进行评估。 Refinements are active until the end of the file or the end of the eval string, respectively. 优化有效,直到分别在文件末尾或eval字符串末尾。

http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html

根据本文 ,类内的改进仅在2.3及更高版本上可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用页面ID /名称加载正确的控制器操作? (Ruby on Rails 4) - How do I use a page id/name to load the correct controller action? (Ruby on Rails 4) 在Ruby on Rails中,如何将控制权传递给另一个控制器中的操作? - In Ruby on Rails, how do I pass control to an action in another controller? 如何使用URL链接到Ruby on Rails中的create操作? - How do I use a URL to link to the create action in Ruby on Rails? Rails:我如何在控制器/动作上使用javascript条件 - Rails: how do I use javascript conditional on controller/action 如何创建Rails控制器动作? - How do I create a rails controller action? 如何将Rails动作或控制器名称插入到Ruby Gem中以在日志文件中使用? - How can I pull in the Rails action or controller names into my Ruby Gem for use in a log file? 如何为 Ruby on Rails 应用程序中的每个控制器/操作自定义内容安全策略 (CSP) 标头? - How do I customize the Content Security Policy (CSP) headers for each controller/action in a Ruby on Rails application? 如何使用curl测试我的Rails控制器创建动作..还是可以使用rspec? - how do I use curl to test my Rails controller create action.. or can I use rspec? 如何使用rails 4中控制器内生成的变量覆盖create action - how do I override create action with variables generated inside the controller in rails 4 Ruby on Rails:如何从 Controller 中获取 Model 名称? - Ruby on Rails: How do I get the Model name from inside the Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM