http://blog.csdn.net/e_online/archive/2009/04/03/4008306.aspx
在Ruby on Rails里面,首先应该明确的一点 —–
Helper是view的helper,根据这个原则,在默认实现下,controller是不允许访问helper里面的方法的。当然helper在本
质上是一个module,所以要在controller和helper之间共享一些方法的办法还是很多的。因为是双方都拥有的方法,所以就面临一个问题
—— 方法放在那里比较合理?controller还是helper?我阐述一下我的原则:
如果方法中需要引入其他helper方法,那么我们建议该方法放在helper中;否则,可以放在controller中或者写一个公共
module。放在controller里面需要注意安全性,因为controller里面public的方法很有可能通过URL加以执行,所以我们建议
所有的helper 方法都用private 加以约束。
下面我就详细说一下4中方法。
Helper方法放在controller中,通过ApplicationHelper来实现公用。代码存在一定的安全隐患。
module ApplicationHelper
def method_missing(method, *args, &block)
if ApplicationController.instance_methods.include? method.to_s
controller.send(method, *args, &block)
else
super
end
end
end
Helper方法放在controller中,通过helper_method来实现公用。代码安全。
private
heper_method :login?
def login?
# your logic goes here
end
Helper方法放在Helper中,通过include module_name向controller暴露,代码安全
include FullModuleName
Helper方法放在一个独立的module中,helper和controller通过include这个module来获得公共方法。
include CommonModuleName
关于include module需要注意的是,它不会帮助你去自动解决module依赖性的问题。
----------------------------------------补充----------------------------------------
Rails默认为每个controller指定一个helper,所有的helper都放在app/helpers目录下
但是有些Helper我们希望是全局共享的,一般我们将这些Helper方法都扔在ApplicationHelper模块里
其实我们可以在app/helpers目录下建立我们自定义的Helper模块,如formatting_helper、path_helper等
-
# formatting_helper.rb
-
module FormattingHelper
-
def free_when_zero(price)
-
price.zero? ?
"FREE"
: number_to_currency(price)
-
end
-
-
def yes_no(bool)
-
bool?
'Yes'
:
'No'
-
end
-
end
-
-
# path_helper.rb
-
module PathHelper
-
def articles_path_for_article(article)
-
if
article.tip?
-
tips_articles_path
-
else
-
news_articles_path
-
end
-
end
-
-
def product_path(product)
-
if
product.kind_of? Book
-
book_path(product)
-
else
-
movie_path(product)
-
end
-
end
-
end
# formatting_helper.rb
module FormattingHelper
def free_when_zero(price)
price.zero? ? "FREE" : number_to_currency(price)
end
def yes_no(bool)
bool? 'Yes' : 'No'
end
end
# path_helper.rb
module PathHelper
def articles_path_for_article(article)
if article.tip?
tips_articles_path
else
news_articles_path
end
end
def product_path(product)
if product.kind_of? Book
book_path(product)
else
movie_path(product)
end
end
end
要想使用这些Helper,我们只需修改ApplicationController即可
-
class
ApplicationController < ActionController::Base
-
helper :formatting, :path
-
end
class ApplicationController < ActionController::Base
helper :formatting, :path
end
或者直接使用
helper :all
来使用所有的Helper
分享到:
相关推荐
gem 'foundation_rails_helper' , '~> 3.0' 然后执行: $ bundle 兼容性 仅完全支持Rails 4.1 / 4.2 / 5/6和Foundation 6 某些功能可能适用于Foundation 5及更早版本,但结果可能会有所不同,并且
在项目目录中运行: $rails s我的方法思考过程当我得到问题陈述时,这些是我最初的指导性问题: 从用户的角度来看,真正的问题是什么? 成功会是什么样? 我需要测试什么才能确保其正常工作? 我将如何对数据建模?...
Summary 94 Chapter 6: Creating Navigation Bars 95 Getting started with a navigation bar 95 Navigation bar helper classes 102 Adding a navigation bar to the Rails application 103 Summary 105 Chapter 7:...
《Rails之道》按照Rails的各个子系统进行组织编排,分别... 《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容...
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
主要介绍了Ruby和Ruby on Rails中解析JSON格式数据的实例教程,文中介绍到的Ruby on Rails中的JSON解析工具包json_pure使用起来十分方便,需要的朋友可以参考下
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
这个 gem 在rails_helper.js.coffee.erb.上的编译时间上嵌入了image_path 、 asset_path和路由表rails_helper.js.coffee.erb. RailsHelper有一些功能。 局限性 这个 gem 只能使用没有命名路由的段。 前任。 # ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
adminlte-rails, AdminLTE Rails gem 将AdminLTE主题与 Rails 资产管道集成 AdminLTE Rails gem AdminLTE 是后端的高级 Bootstrap 主题。英镑 AdminLTE Rails gem 与 Rails 资产管道集成了英镑AdminLTE主题。安装将...
中文世界唯一一本Rails 4.0.0 + Ruby 2.0.0 的自學書籍
rails文档 rails api 英文
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
rails指南 中文版
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...