Rails 3 View: Difference between revisions

From Wiki
Jump to navigation Jump to search
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 16: Line 16:
render :layout => false
render :layout => false
</source>
</source>
=== Form-with-object Example ===
<source lang="ruby">
<%= form_for(@article) do |f| %>
<% if @article.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@article.errors.count, "error") %>
prohibited this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :excerpt %><br />
<%= f.text_field :excerpt %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :published_at %><br />
<%= f.datetime_select :published_at %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</source>
Parameters are sent to the controller in <code>params[:article]</code>.
=== Anonymous form example ===
<source lang="ruby">
<%= form_tag('/runs/update') do %>
    <tr><td>treadmill time: <%= text_field_tag :treadmill_time %></td>
    <td>treadmill miles: <%= text_field_tag :treadmill_miles %></td></tr>
    <tr><td colspan="2"><%= hidden_field_tag :id, @run.id %><%= submit_tag('Save') %></td></tr>
<% end %>
</source>
=== Partials and Files ===
Examples called in something like <code>app/views/articles/edit.html.erb</code>
<source lang="ruby">
<%= render 'form' %>
</source>
renders <code>app/views/articles/_form.html.erb</code>
<source lang="ruby">
<%= render 'header', :title => 'My Blog' %>
</source>
renders <code>app/views/articles/_header.html.erb</code> and passes local variable <code>title</code>
<source lang="ruby">
<%= render @article %>
</source>
Rails looks for a partial in <code>app/views/articles/_article.html.erb</code> and automatically assigns a local variable called article. It's a shortcut for
<source lang="ruby">
<%= render 'article', :article => @article %>
</source>
<source lang="ruby">
<%= render @articles %>
</source>
equivalent to
<source lang="ruby">
<% @articles.each do |object| %>
<%= render object %>
<% end %>
</source>
If your partial is more like a full-fledged action, you can render it as a file:
<source lang="ruby">
<%= render :file => 'comments/new' %>
</source>
which renders <code>app/views/comments/new.html.erb</code>.

Latest revision as of 18:50, 6 December 2011

Helpers are intended to generate complicated markup for use in the associated views.

Layouts

  • The default layout for the entire application is app/views/layouts/application.html.erb.
  • If controller-specific layouts are found, they are used instead.
  • You can use the layout directive at the class level in any controller (that is, not inside an action) to set the layout for the entire controller:
layout 'my_layout'
  • You can include a layout for a specific action with an explicit call to render inside the action:
render :layout => 'my_layout'
  • Sometimes, you want to render an action without a layout. In that case, you can pass false in place of the layout name:
render :layout => false

Form-with-object Example

<%= form_for(@article) do |f| %> 
<% if @article.errors.any? %> 
<div id="errorExplanation"> 
<h2><%= pluralize(@article.errors.count, "error") %>
prohibited this article from being saved:</h2> 
<ul> 
<% @article.errors.full_messages.each do |msg| %> 
<li><%= msg %></li> 
<% end %> 
</ul> 
</div> 
<% end %>

<div class="field"> 
<%= f.label :title %><br /> 
<%= f.text_field :title %> 
</div>
<div class="field"> 
<%= f.label :location %><br /> 
<%= f.text_field :location %> 
</div> 
<div class="field"> 
<%= f.label :excerpt %><br /> 
<%= f.text_field :excerpt %> 
</div> 
<div class="field"> 
<%= f.label :body %><br /> 
<%= f.text_area :body %> 
</div> 
<div class="field"> 
<%= f.label :published_at %><br /> 
<%= f.datetime_select :published_at %> 
</div> 
<div class="actions"> 
<%= f.submit %> 
</div> 
<% end %>

Parameters are sent to the controller in params[:article].

Anonymous form example

<%= form_tag('/runs/update') do %>
    <tr><td>treadmill time: <%= text_field_tag :treadmill_time %></td>
    <td>treadmill miles: <%= text_field_tag :treadmill_miles %></td></tr>
    <tr><td colspan="2"><%= hidden_field_tag :id, @run.id %><%= submit_tag('Save') %></td></tr>
<% end %>

Partials and Files

Examples called in something like app/views/articles/edit.html.erb

<%= render 'form' %>

renders app/views/articles/_form.html.erb

<%= render 'header', :title => 'My Blog' %>

renders app/views/articles/_header.html.erb and passes local variable title

<%= render @article %>

Rails looks for a partial in app/views/articles/_article.html.erb and automatically assigns a local variable called article. It's a shortcut for

<%= render 'article', :article => @article %>
<%= render @articles %>

equivalent to

<% @articles.each do |object| %> 
<%= render object %> 
<% end %>

If your partial is more like a full-fledged action, you can render it as a file:

<%= render :file => 'comments/new' %>

which renders app/views/comments/new.html.erb.