Rails宝典之第四十三式: Ajax with RJS

来看看RJS的应用

我们首先要加上*.js文件,如修改application.rhtml:
[code]
<%= javascript_include_tag :defaults %>
[/code]
然后给Review的create方法加上js格式的应答:
[code]
def create
@review = Review.create!(params[:review])
flash[:notice] = "Thank you for reviewing this product"
respond_to do |format|
format.html { redirect_to product_path(@review.product_id) }
format.js
end
end
[/code]
我们在创建Review的页面中使用form_remote_for:
[code]
<!-- products/show.rhtml -->
<% form_remote_for :review, :url => reviews_path, :html => { :id => 'review_form' } do |f| %>
...
<% end %>
[/code]
最后就是写create.rjs文件了:
[code]
page.insert_html :bottom, :reviews, :partial => 'review', : object => @review
page.replace_html :reviews_count, pluralize(@review.product,reviews.size, 'Review')
page[:review_form].reset
page.replace_html :notice, flash[:notice]
flash.discard
[/code]
没多大新意,学习RJS看<<RJS Templates for Rails>>即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值