Rendering in Rails

[b]参考链接:[url]http://guides.rubyonrails.org/layouts_and_rendering.html[/url][/b]

[color=red][b]render[/b][/color]

如果action中没有调用render、redirect_to、head和method_missing方法中的任何一个,rails默认会去渲染和当前action名字相对应的那个模板。比如一个BooksController里有这样一段代码:
def show
@book = Book.find(params[:id])
end

rails将会在执行完show方法后渲染 app/views/books/show.html.erb 模板。

[quote]If you want to see the exact results of a call to render without needing to inspect it in a browser, you can call [b]render_to_string[/b]. This method takes exactly the same options as render, but it returns a string instead of sending a response back to the browser.[/quote]

如果你希望给浏览器发送一个空的响应信息,可以使用[b]:nothing选项[/b]:
render :nothing=>true

render的第一个参数如果是一个string或者symbol,rails则会去渲染同一个controller下的某action对应的模板,比如:
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
render "edit" #OR render :edit
end
end
end

上面的代码在@book.update_attributes方法返回false时,将会去渲染这个controller中edit这个action对应的模板。下面的代码有同样的效果,只是在Rails2.3中没必要写得这么麻烦:
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
render :action=>'edit'
end
end
end

如果要渲染的是另一个controller的模板,可以在render方法的参数中直接指定那个模板的路径(相对于app/views的相对路径),比如说你要在app/controllers/admin下的AdminProductsController中渲染app/views/products/show.html.erb模板,可以这样调用render:
render 'products/show'

下面的代码有同样的效果,只是在Rails2.3中没必要写得这么麻烦:
render :template => 'products/show' 

还可以渲染其它任意位置的模板文件,比如说在某种情况下,两个rails应用共享了同一个模板文件:
render "/u/apps/warehouse_app/current/app/views/products/show"

rails根据参数的第一个字符是否"/"来判断这是不是一个file render(怎么翻译? - -)。
下面的代码有同样的效果,只是在Rails2.3中没必要写得这么麻烦:
render :file => "/u/apps/warehouse_app/current/app/views/products/show"

默认的,file render不使用当前的layout,如果你打算让它使用当前的layout,必须指定:layout=>true。

[b][color=red]注意:[/color][/b]如果你的rails运行在Microsoft Windows上,则必须使用:file选项来渲染文件,因为Windows文件名的格式和Unix不一样。

[b]:inline选项[/b]可以让render方法渲染一个字符串,而不是一个erb模板:
render :inline => "<% products.each do |p| %><p><%= p.name %><p><% end %>" 

几乎没有什么理由使用:inline这个选项,在controller逻辑中混入ERB代码违反了Rails的MVC原则并且使其它开发者难以理解你的程序逻辑。最好是用分离了"逻辑"和"表现"的erb模板来代替:inline选项。
inline rendering默认使用ERB,你也可以通过指定:type选项来强制它使用Builder:
render :inline => "xml.p {'Horrid coding practice!'}", :type => :builder

[quote][b]Using render with :update[/b]

You can also render javascript-based page updates inline using the :update option to render:
render :update do |page|
page.replace_html 'warning', "Invalid options supplied"
end


Placing javascript updates in your controller may seem to streamline small updates, but [b][color=red]it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. We recommend using a separate rjs template instead, no matter how small the update.[/color][/b][/quote]
通过[b]:text选项[/b]可以把一个纯文本内容发送给浏览器:
render :text =>'hello'

[quote]Rendering pure text is most useful when you’re responding to AJAX or web service requests that are expecting something other than proper HTML.[/quote]
和file render一样,默认的,:text选项不使用当前的layout,如果你打算让它使用当前的layout,必须指定:layout=>true。

[b]渲染json[/b]
很简单:
render :json=>@product

[quote]You don’t need to call to_json on the object that you want to render. If you use the :json option, render will automatically call to_json for you.[/quote]
[b]渲染xml[/b]
同json:
render :xml=>@product
后面的那小段英文就不多引用了,一样。
[b]Rendering Vanilla JavaScript[/b]
render :js => "alert('Hello Rails');"

[b]Options for render[/b]
render方法还接受4个选项:
:content_type
:layout
:status
:location
[b]:content_type[/b]
[quote]By default, Rails will serve the results of a rendering operation with the MIME content-type of text/html (or application/json if you use the :json option, or application/xml for the : xml option.). There are times when you might like to change this, and you can do so by setting the :content_type option:
render :file => filename, :content_type => 'application/rss'
[/quote]
[b]:layout[/b]
[quote]You can use the :layout option to tell Rails to use a specific file as the layout for the current action:
render :layout => 'special_layout'

You can also tell Rails to render with no layout at all:
render :layout => false 
[/quote]
[b]:status[/b]
[quote]Rails will automatically generate a response with the correct HTML status code (in most cases, this is 200 OK). You can use the :status option to change this:
render :status => 500 render :status => :forbidden

Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in [b]actionpack/lib/action_controller/status_codes.rb[/b]. You can also see there how Rails maps symbols to status codes.[/quote]
[b]:location[/b]
[quote]You can use the :location option to set the HTTP Location header:
render :xml => photo, :location => photo_url(photo)
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值