`
sillycat
  • 浏览: 2543473 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Rails Study(V)Layouts and Rendering

 
阅读更多
Rails Study(V)Layouts and Rendering

2.4 Using head To Build Header-Only Responses
The head method can be used to send responses with only headers to the browser.
head :bad_request   # error header message  HTTP/1.1 400 Bad Request

Or we can use other HTTP headers to convey additional information:
head :created, :location => photo_path(@photo) # Location: /photos/1

3. Structuring Layouts
Within a layout, you have access to three tools for combining different bits of output to form the overall response:
* Asset tags
* yield and content_for
* Partials

3.1 Asset Tags
Asset tags provide methods for generating HTML that links views to feeds, JavaScript, stylesheets, images, videos and audios.
* auto_discovery_link_tag
* javascript_include_tag
* stylesheet_link_tag
* image_tag
* video_tag
* audio_tag

3.1.1 Linking to Feeds with auto_discovery_link_tag
3.1.2 Linking to Javascript Files with javascript_include_tag
The javascript_include_tag helper returns an HTML script tag for each source provided.
Rails looks in public/javascripts for these files by default, but you can specify a full path relative to the document root, or a URL.

<%= javascript_include_tag "main" %>  #public/javascripts/main.js

<%= javascript_include_tag "main", "columns" %>  #public/javascripts/main.js and public/javascripts/columns.js

<%= javascript_include_tag "main", "/photos/columns" %> #public/javascripts/main.js and public/photos/columns.js

<%= javascript_include_tag "http://example.com/main.js" %> #

<%= javascript_include_tag :defaults %> # The defaults option loads the Prototype and Scriptaculous libraries.

<%= javascript_include_tag :all %> # The all option loads every javascript file in public/javascripts, starting with the Prototype and Scriptaculous libraries.

<%= javascript_include_tag :all, :recursive => true %> # load files in subfolders of public/javascripts as well.

<%= javascript_include_tag "main", "columns", :cache => true %>
Create a better user experience by combining multiple files into a single download.

3.1.3 Linking to CSS Files with stylesheet_link_tag
Rails looks in public/stylesheets for these files by default.
<%= stylesheet_link_tag "main" %>   #public/stylesheets/main.css

<%= stylesheet_link_tag "main", "column" %> #public/stylesheets/main.css and public/stylesheets/columns.css

<%= stylesheet_link_tag "main", "/photos/columns" %> # public/stylesheets/main.css and public/photos/columns.css

<%= stylesheet_link_tag "http://example.com/main.css" %> #

<%= stylesheet_link_tag "main_print", :media => "print" %>
By default, stylesheet_link_tag creates links with media="screen" ref="sytlesheet" type="text/css". Option(:media, :rel, :type)

<%= stylesheet_link_tag :all %>

<%= stylesheet_link_tag :all, :recursive => true %>

<%= stylesheet_link_tag "main", "columns", :cache => true %>

<%= stylesheet_link_tag "main", "columns", :cache => 'cache/main/display' %>
or
<%= stylesheet_link_tag "main", "columns", :cache => 'cache/#{current_site}/main/display' %>

3.1.4 Linking to Images with images_tag
By default, files are loaded from public/images.

<%= image_tag "header.png" %>
<%= image_tag "icons/delete.gif" %> # supply a path to the image
<%= image_tag "icons/delete.gif", {:height => 45} %>
<%= image_tag "home.gif",nmouseover => "menu/home_highlight.gif" %>

<%= image_tag "home.gif", :alt => "Home" %>
<%= image_tag "home.gif", :size => "50x20" %>

<%= image_tag "home.gif", :alt => "Go Home",
                                        :id => "HomeImage",
                                        :class => 'nav_bar' %>

3.1.5 Linking to Videos with video_tag
3.1.6 Linking to Audio files with audio_tag

3.2 Understanding yield
Within the context of a layout, yield identifies a section where content from the view should be inserted.

Layout with simple regions.
<html>
   <head>
   </head>
   <body>
   <%= yield %>
   </body>
</html>

Layout with multiple yielding regions.
<html>
   <head>
   <%= yield :head %>
   </head>
   <body>
   <%= yield %>
   </body>
</html>
The main body of the view will always render into the nunamed yield. To render content into a named yield, use content_for method.

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

3.4 Using Partials
3.4.1 Naming Partials
To render a partial as part of a view, you use the render method within the view:
<%= render "menu" %>
This will render a file named _menu.html.erb at that point within the view being rendered.

<%= render "shared/menu" %>
Pull in a partial from another folder, that code will pull in the partial from app/views/shared/_menu.html.erb

3.4.2 Using Partials to Simplify Views
<%= render "shared/ad_banner" %>
<h1>Products</h1>
...
<%= render "shared/footer" %>

For content that is shared among all pages in your application, you can use partials directly from layouts.

3.4.3 Partial Layouts
<%= render "link_area", :layout => "graybar" %>
This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb.
The layout is placed in the same folder with the partial that they belong to.

3.4.4 Passing Local Variables
new.html.erb
<h1>New zone</h1>
<%= error_messages_for :zone %>
<%= render :partial => "form", :locals => { :zone => @zone } %>

edit.html.erb
<h1>Editing zone</h1>
<%= error_messages_for :zone %>
<%= render :partial => "form", :locals => { :zone => @zone } %>

_form.html.erb
<%= form_for(zone) do |f| %> 
<p>    
<b>Zone name</b><br />    
<%= f.text_field :name %> 
</p>  
<p>    
<%= f.submit %> 
</p>
<% end %>

3.4.5 Rendering Collections
index.html.erb
<h1>Products</h1>
<%= render :partial => "product", :collection => @products %>

_product.html.erb
<p>Product Name: <%= product.name %></p>

short to
<h1>Products</h1>
<%= render @products %>

or
index.html.erb
<%= render [customer1, employee1, customer2, employee2] %>

customers/_customer.html.erb
<p>Customer: <%= customer.name %></p>

employees/_employee.html.erb
<p>Employee: <%= employee.name %></p>

3.4.6 Local Variables
To use a custom local variable name within the partial, specify the :as option in the call to the partial:
<%= render :partial => "product", :collection => @products, :as => :item %>

We can access an instance of the @products collection as the item local variable within the partial.

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

3.4.7 Spacer Templates
<%= render @products, :spacer_template => "product_ruler" %>
Specify a second partial to be rendered between instances of the main partial.
Rails will render the _product_ruler partial (with no data passed in to it) between each pair of _product partials.

3.5 Using Nested Layouts
app/views/layouts/application.html.erb
<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>

On pages generated by NewController, want another layout
app/views/layouts/news.html.erb
<% 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' %>

references:
http://guides.rubyonrails.org/layouts_and_rendering.html
http://guides.rubyonrails.org/active_record_querying.html
分享到:
评论

相关推荐

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...

    Ruby on Rails_ Up and Running

    RUBY的经典之作,对其在RAILS下开发写得很详细

    Ruby on Rails 指南 v5.0.1 中文版

    ### Ruby on Rails 指南 v5.0.1 中文版 #### Rails入门 - **前提条件**:为了能够顺利地开始Rails的学习之旅,读者需要具备一定的Ruby语言基础,并且对Web开发有一定的了解。 - **Rails是什么?**:Rails是一种...

    ruby on rails guides v5.0.0.1

    ruby on rails 开发指南

    rails-layouts-and-templates-lab-v-000

    Rails布局和模板实验室 目标 您的任务是建立在线商店! 好的,也许不是整个在线商店,但至少是一些在线商店的布局和控制器。 您不仅将学习如何创建布局以及如何获得使用该布局的操作,而且还将学习如何覆盖默认值并...

    rails2-sample

    Helpers, Forms, and Layouts(辅助方法、表单和布局) Rails提供了一系列辅助方法来简化HTML的生成和页面的布局。同时,表单是Web应用中不可或缺的部分,Rails提供了丰富的API来处理表单的创建和验证。此外,布局...

    ruby.on.rails.up.and.running

    《Ruby on Rails Up and Running》是一本专注于介绍Ruby on Rails框架的书籍,旨在帮助开发者快速上手并深入了解这个强大的Web开发平台。Ruby是一种面向对象的编程语言,以其简洁、优雅的语法著称,而Rails是基于...

    Rails3 device and cancan

    标题《Rails3 device and cancan》与描述《ROR ruby on rails device plugin教程》指出本文是关于如何在Rails 3.2应用程序中整合Devise认证插件和Cancan授权插件的教程。Devise是一个流行的Ruby on Rails的认证解决...

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

    Agile Web Development with Rails 4

    You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...

    The Rails3 Way, 2nd Edition

    Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...

    Ruby on Rails: Up and Running

    《Ruby on Rails: Up and Running》是一本针对初学者和有经验开发者的技术书籍,它深入浅出地介绍了如何使用Ruby on Rails框架构建Web应用程序。Ruby on Rails(简称Rails)是基于Ruby编程语言的一个开源Web应用框架...

    rails-layouts-and-templates-lab-london-web-091619

    Rails布局和模板实验室 目标 您的任务是建立在线商店! 好的,也许不是整个在线商店,但至少是一些在线商店的布局和控制器。 您不仅将学习如何创建布局,以及如何获得使用该布局的动作,而且还将学习如何覆盖默认值...

    rails-layouts-and-templates-lab-london-web-021720

    Rails布局和模板实验室目标您的任务是建立在线商店! 好的,也许不是整个在线商店,但至少是一些在线商店的布局和控制器。 您不仅将学习如何创建布局,以及如何获得使用该布局的动作,而且还将学习如何覆盖默认值并...

    inspinia admin - v2.5 Rails_Full_Version

    "inspinia admin - v2.5 Rails_Full_Version" 是一个基于Rails框架构建的后台管理系统的完整版本。这个系统采用流行的Inspinia Admin模板,提供了丰富的功能和自定义选项,旨在帮助开发者快速构建高效、现代且用户...

    js-rails-as-api-rendering-different-content-types-using-rails-v-000

    在Rails中渲染不同的内容类型 学习目标 覆盖默认的Rails视图 从Rails控制器渲染纯文本 从Rails控制器渲染JSON 介绍 在上一课中,我们重新讨论了默认的Rails MVC结构,最后,呈现了一个ERB文件。 但是,Rails可以呈现...

Global site tag (gtag.js) - Google Analytics