现在的位置: 首页 > 综合 > 正文

rails layout and rendering

2014年01月10日 ⁄ 综合 ⁄ 共 2816字 ⁄ 字号 评论关闭

Using render 

You
can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON,
or XML.
You can specify the content type or HTTP status
of the rendered response as well.


render :nothing => true

We see there is an empty response (no data after the Cache-Control line),
but the request was successful because Rails has set the response to
200 OK.
You can set the 
:status option
on render to change this response. Rendering nothing can be useful for 
AJAX requests

where all you want to send back to the browser is
an acknowledgement that the request was completed.


Any
instance variables that you require in the view must be set up in the current action before calling render.


render "/u/apps/warehouse_app/current/app/views/products/show"

By default, the file is rendered without using the current layout. If you want Rails to put the file into the current
layout, you need to add the :layout
=> true
 option.


render :inline =>
  "<% products.each do |p| %><p><%= p.name %><p><% end %>"

render :inline =>
  "xml.p {'Horrid coding practice!'}", :type => :builder

render :update do |page|
  page.replace_html 'warning', "Invalid options supplied"
end

By default, if you use the :text option
the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the:layout
=> true
 option.


Calls to the render method
generally accept four options:

    :content_type
    :layout
    :status
    :location
    render :status => 500
    render :status => :forbidden
    

    To assign a specific layout for the entire application, use a declaration in yourApplicationController class:

    class ApplicationController < ActionController::Base
      layout "main"
      #...
    end

    Choosing Layouts at Runtime

    class ProductsController < ApplicationController
      layout :products_layout
     
      def show
        @product = Product.find(params[:id])
      end
     
      private
        def products_layout
          @current_user.special? ? "special" : "products"
        end
     
    end

    You can also decide the layout by passing a Proc object, the block you give the Proc will be given the controller instance,
    so you can make decisions based on the current request. For example:

    class ProductsController < ApplicationController
      layout Proc.new { |controller| controller.request.xhr? ? 'popup' : 'application' }
    end

    Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones.

    def show
      @book = Book.find(params[:id])
      if @book.special?
        render :action => "special_show" and return
      end
      render :action => "regular_show"
    end

    Rails uses HTTP status
    code 302 (temporary redirect) when you call redirect_to.
    If you’d like to use a different status code (perhaps 301, permanent redirect), you can do so by using the :statusoption:

    head :bad_request

    head :created, :location => photo_path(@photo)

    Structuring Layouts

    Asset tags
    yield and content_for
    Partials

    Asset Tags

    auto_discovery_link_tag
    javascript_include_tag
    stylesheet_link_tag
    image_tag
    video_tag
    audio_tag

    Using content_for and Understanding yield

    <html>
      <head>
      <%= yield :head %>
      </head>
      <body>
      <%= yield %>
      </body>
    </html>

    <% content_for :head do %>
      <title>A simple page</title>
    <% end %>
     
    <p>Hello, Rails!</p>

    <html>
      <head>
      <title>A
    simple page</
    title>
      </head>

    抱歉!评论已关闭.