Rendering in Rails

参考链接:http://guides.rubyonrails.org/layouts_and_rendering.html

render

如果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 模板。

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


如果你希望给浏览器发送一个空的响应信息,可以使用 :nothing选项
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。

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

:inline选项可以让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

引用
Using render with :update

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 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.

通过 :text选项可以把一个纯文本内容发送给浏览器:
render :text =>'hello'

引用
Rendering pure text is most useful when you’re responding to AJAX or web service requests that are expecting something other than proper HTML.

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

渲染json
很简单:
render :json=>@product

引用
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.

渲染xml
同json:
render :xml=>@product
后面的那小段英文就不多引用了,一样。
Rendering Vanilla JavaScript
render :js => "alert('Hello Rails');"

Options for render
render方法还接受4个选项:
:content_type
:layout
:status
:location
:content_type
引用
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'

:layout
引用
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 

:status
引用
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 actionpack/lib/action_controller/status_codes.rb. You can also see there how Rails maps symbols to status codes.

:location
引用
You can use the :location option to set the HTTP Location header:
render :xml => photo, :location => photo_url(photo)

你可能感兴趣的:(JavaScript,mvc,json,Ruby,Rails)