Rails宝典之六十九式: Markaby in Helper

看一个打印error_message相关的html的helper:
[code]
module ApplicationHelper
def simple_error_message_for(object_name)
object = instance_variable_get("@#{object_name}")
return if object.errors.empty?

result = '<div id="error_message">'
result += "<h2>#{pluralize(object.errors.count, 'error')} occurred</h2>"
result += "<p>There were problems with the following fields:</p>"
result += "<ul>"
object.errors.each_full do |msg|
result += "<li>#{msg}</li>"
end
result += "</ul>"
result += "</div>"
result
end
end
[/code]

这么多html标签,写成String会灰常郁闷,而markaby库就是为简化我们的html标签的
或者说markaby是一种模板语言,它以插件的形式安装:
[code]
script/plugin install http://code.whytheluckystiff.net/svn/markaby/trunk
[/code]

看看简化后的样子:
[code]
# in helper
def simple_error_messages_for(object_name)
object = instance_variable_get("@#{object_name}")
return if object.errors.empty?

markaby do
div.error_messages! do
h2 "#{pluralize(object.errors.count, 'error')} occurred"
p "There were problems with the following fields:"
ul do
object.errors.each_full do |msg|
li msg
end
end
end
end
end

def markaby(&block)
Markaby::Builder.new({}, self, &block)
end
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值