rails layout and rendering

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 >
       < body >
       < p >Hello, Rails!</ p >
       </ body >
    </ html >
    The main body of the view will always render into the unnamed yield. To render content into a named yield, you use the content_for method.

    Using Partials
    Partial Layouts
    A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
    <%= render "link_area", :layout => "graybar" %>
    


    Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the :object option:
    <%= render :partial => "customer", :object => @new_customer %>
    

    <h1>Products</h1>
    <%= render :partial => "product", :collection => @products %>
    

    When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial . In this case, the partial is _product, and within the _product partial, you can refer to product to get the instance that is being rendered.

    You can also pass in arbitrary local variables to any partial you are rendering with the :locals => {} option:
    <%= render :partial => 'products', :collection => @products,
               :as => :item, :locals => {:title => "Products Page"} %>
    

    Spacer Templates
    <%= render @products, :spacer_template => "product_ruler" %>
    
    Using Nested Layouts
    <html>
    <head>
      <title><%= @page_title or 'Page Title' %></title>
      <%= stylesheet_link_tag 'layout' %>
      <style type="text/css"><%= yield :stylesheets %></style>
    </head>
    <body>
      <div id="top_menu">Top menu items here</div>
      <div id="menu">Menu items here</div>
      <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
    </body>
    </html>

    <% content_for :stylesheets do %>
      #top_menu {display: none}
      #right_menu {float: right; background-color: yellow; color: black}
    <% end %>
    <% content_for :content do %>
      <div id="right_menu">Right menu items here</div>
      <%= yield(:news_content) or yield %>
    <% end %>
    <%= render :file => 'layouts/application' %>



    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值