简体   繁体   English

如何在Rails 3.2的视图和控制器中使用下划线方法?

[英]How to make underscore method available in view and controller in rails 3.2?

There is underscore method in ActiveSupport::Inflector ( http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore ). ActiveSupport::Inflectorunderscore方法( http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore )。 But it is not available in both erb view and rails controller. 但是在erb视图和rails控制器中均不可用。 How can we make it available in view or in rails controller? 我们如何使其在视图或Rails控制器中可用?

This method is added to String. 此方法已添加到String。 You should be able to call it on any String object, in any context (controller, view, etc.) assuming you've loaded ActiveSupport 假设您已加载ActiveSupport,则应该能够在任何上下文(控制器,视图等)中的任何String对象上调用它

If you are using rails 4 you should do the following 如果使用滑轨4,则应执行以下操作

  1. go to the controler then follow this example: 请转到控制程序,然后按照以下示例操作:

    def new def新

    @about = About.new @about = About.new

    @delete_me = 'EggsAndHam'.underscore @delete_me ='EggsAndHam'。下划线

    end 结束

you can call the @delete_me on your view 您可以在视图上调用@delete_me

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

相关问题 Rails方法在View中不可用 - Rails Method not available in View 如何在Rails中为我的控制器和模型提供方法? - How do I make a method available to both my controller and model in Rails? 如何使在Rails引擎的应用程序控制器中定义的方法自动适用于安装它的任何其他引擎? - How to make method defined in a rails engine's application controller automatically available for any other engines which mount it? 如何使用devise和rails 3.2使用户从控制器登录? - How to make a User signed_in from controller using devise and rails 3.2? 如何为导轨 controller 提出请求规格 - 请求不符合 controller 方法 - How to make request specs for rails controller - request not hitting controller method Rails 3类可供查看,控制器和模型 - Rails 3 class available to view, controller and model 如何在Rails中为多个模型提供相同的方法? - How can I make the same method available to multiple models in Rails? 如何在Rails中仅使10分钟的“编辑发布”方法可用? - How to make “edit post” method available for only 10 minutes in Rails? Rails:如何在setter方法中提供父属性 - Rails: How to make available parent attribute in setter method 如何让工厂可以使用辅助方法? (导轨,Rspec) - How do I make a helper method available to factories? (Rails, Rspec)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM