RJS与ActionController

本文介绍在 Rails 应用中使用 RJS (Ruby/JavaScript) 的多种方法,包括如何通过指定 renderrjs 来避免其他模板提前渲染,如何使用 respond_to 指定不同格式的响应,以及在渲染 RJS 模板时自动 skip layouts 的特性。同时介绍了 Inline Rendering 和 Browser Redirection 的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,可以通过指定render rjs来避免其他模板在rjs前render
[code]
def product
# skip product.rhtml or product.rxml if either exists
render :action => "product.rjs"
[/code]

2,可以通过respond_to来指定不同的format
[code]
def product
respond_to do |format|
format.html {flash["notice"] = 'here is a product'}
format.js {render :action => "product.rjs"}
format.xml {render :xml => @product.to_xml}
[/code]

3,在渲染RJS模板时ActionController会自动skip layouts,所以不用指定:layout => false

4,rendering options
Option Work with RJS? Returns Content-Type=text/javascript?
:action expected Yes
:template expected Yes
:file expected Yes
:inline not useful No
:partial not useful Yes
:text not useful No

5,Inline Rendering
[code]
def update
render :update do |page|
page.replace_html, 'header', :partial => 'header'
end
end
[/code]

6,Browser Redirection
[code]
render :update do |page|
page.redirect_to :controller => 'employees', :action => 'list'
end

render :update do |page|
page.redirect_to 'http://www.shopify.com'
end
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值