每天一剂Rails良药之simply_helpful

本文介绍了Simply_Helpful插件如何简化Rails应用开发,包括简化渲染部分模板的方法、生成DOM ID和类名的方式以及form_for语法的增强。这些改进使得开发者能够更高效地编写代码。
simply_helpful插件为我们添加了许多helper方法,例如:
1,render partial
以前我们这样写:
[code]
<table>
<tr><th>Name</th><th>City</th><th>Postcode</th></tr>
<%= render :partial => 'venue', :collection => @venues %>
</table>
[/code]
现在可以这样写:
[code]
<table>
<tr><th>Name</th><th>City</th><th>Postcode</th></tr>
<%= render :partial => @venues %>
</table>
[/code]

2,DOM ID/DOM Class names
[code]
<%= dom_id(object, prefix = nil) %>
[/code]
生成的DOM对象的ID的格式为person_123或者new_person
我们在RJS里也可以使用该helper方法,如下三种方式是等同的:
[code]
page[:person_123]
page[dom_id(@person)]
page[@person]
[/code]

3,Form block
新的form_for语法与RESTful Routes集成来大大简化form代码:
1,form的action是update还是create取决于对象以前是否save过
2,form自动得到一个id,如new_venue或edit_venue_123,这取决于form为new还是update
3,form自动得到一个class name,如new_venue或edit_venue
这样一来,form_for的动态性让我们可以只用一行代码搞定new和edit两种表单:
[code]
<% form_for @venue do |f| %>
[/code]

BTW:simply_restful began its life as a plugin, and at RailsConf, the core team announced that it would be merged into Rails core.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值