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
      评论
    Rails(也称为Ruby on Rails)是一个用于构建Web应用程序的开发框架,它基于Ruby编程语言。Rails具有简洁的语法和强大的约定,使开发人员可以快速构建功能丰富的应用程序。它提供了一套固定的文件结构和开箱即用的功能,如数据库集成、用户认证和路由。 Angular是一个用于构建Web前端应用程序的JavaScript框架。它采用了一种称为MVVM(Model-View-ViewModel)的设计模式,使开发人员能够创建动态和交互性的用户界面。Angular具有丰富的功能,如数据绑定、组件化和依赖注入,使开发人员可以更轻松地构建复杂的前端应用程序。 PostgreSQL是一种开源的关系型数据库管理系统(RDBMS),它具有强大的性能和可扩展性。PostgreSQL支持SQL标准,并提供了许多高级功能,如事务、视图和触发器。它还包含了许多内置的数据类型和操作符,使开发人员能够更有效地处理和查询数据。 Bootstrap是一个流行的前端框架,用于构建响应式和美观的Web界面。它提供了一套CSS和JavaScript组件,使开发人员能够快速创建网站和应用程序。Bootstrap具有强大的网格系统和预定义的样式,使开发人员能够轻松地进行布局和设计。 这四个技术在Web开发中经常被同时使用,可以相互配合使用来构建完整的Web应用程序。Rails提供了后端开发的框架和工具,Angular提供了前端开发的框架和工具,PostgreSQL提供了可靠的数据库管理系统,而Bootstrap提供了美观和响应式的用户界面。通过使用这些技术,开发人员可以更高效地构建功能强大和用户友好的Web应用程序。

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值