Skip to content

Commit

Permalink
Add update post section to getting started guide
Browse files Browse the repository at this point in the history
  • Loading branch information
oscardelben committed Apr 24, 2012
1 parent 5acb345 commit f0de717
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 4 deletions.
14 changes: 14 additions & 0 deletions guides/code/getting_started/app/controllers/posts_controller.rb
Expand Up @@ -21,4 +21,18 @@ def create
render 'new'
end
end

def edit
@post = Post.find(params[:id])
end

def update
@post = Post.find(params[:id])

if @post.update_attributes(params[:post])
redirect_to :action => :show, :id => @post.id
else
render 'edit'
end
end
end
2 changes: 1 addition & 1 deletion guides/code/getting_started/app/views/posts/_form.html.erb
@@ -1,4 +1,4 @@
<%= form_for :post, :url => { :action => :create } do |f| %>
<%= form_for :post, :url => { :action => :update, :id => @post.id }, :method => :put do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
Expand Down
3 changes: 1 addition & 2 deletions guides/code/getting_started/app/views/posts/edit.html.erb
Expand Up @@ -2,5 +2,4 @@

<%= render 'form' %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
<%= link_to 'Back', :action => :index %>
2 changes: 2 additions & 0 deletions guides/code/getting_started/app/views/posts/index.html.erb
Expand Up @@ -7,13 +7,15 @@
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
</tr>

<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', :action => :show, :id => post.id %>
<td><%= link_to 'Edit', :action => :edit, :id => post.id %>
</tr>
<% end %>
</table>
1 change: 1 addition & 0 deletions guides/code/getting_started/app/views/posts/show.html.erb
Expand Up @@ -9,3 +9,4 @@
</p>

<%= link_to 'Back', :action => :index %>
| <%= link_to 'Edit', :action => :edit, :id => @post.id %>
2 changes: 2 additions & 0 deletions guides/code/getting_started/config/routes.rb
Expand Up @@ -7,6 +7,8 @@
get "posts/new"
post "posts/create"
get "posts/:id" => "posts#show"
get "posts/:id/edit" => "posts#edit"
put "posts/:id/update" => "posts#update"

# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down
132 changes: 131 additions & 1 deletion guides/source/getting_started.textile
Expand Up @@ -726,7 +726,7 @@ something went wrong. To do that, you'll modify
+app/views/posts/index.html.erb+ to check for error messages:

<erb>
<%= form_for :post do |f| %>
<%= form_for :post, :url => { :action => :create } do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
Expand Down Expand Up @@ -776,6 +776,136 @@ Now you'll get a nice error message when saving a post without title:

!images/getting_started/form_with_errors.png(Form With Errors)!

h4. Updating Posts

We've covered the "CR" part of CRUD. Now let's focus on the "U" part,
updating posts.

The first step we'll take is adding a +edit+ action to
+posts_controller+.

Start by adding a route to +config/routes.rb+:

<ruby>
get "posts/:id/edit" => "posts#edit"
</ruby>

And then add the controller action:

<ruby>
def edit
@post = Post.find(params[:id])
end
</ruby>

The view will contain a form similar to the one we used when creating
new posts. Create a file called +app/views/posts/edit.html.erb+ and make
it look as follows:

<erb>
<h1>Editing post</h1>

<%= form_for :post, :url => { :action => :update, :id => @post.id },
:method => :put do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>

<p>
<%= f.submit %>
</p>
<% end %>

<%= link_to 'Back', :action => :index %>
</erb>

This time we point the form to the +update+ action (not defined yet).
The +:method => :put+ option tells Rails that we want this form to be
submitted via +put+, which is the http method you're expected to use to
*update* resources according to the REST protocol.

TIP: By default forms built with the +form_for_ helper are sent via +POST+.

Moving on, we need to add the +update+ action. The file
+config/routes.rb+ will need just one more line:

<ruby>
put "posts/:id/update"
</ruby>

And the +update+ action in +posts_controller+ itself should not look too complicated by now:

<ruby>
def update
@post = Post.find(params[:id])

if @post.update_attributes(params[:post])
redirect_to :action => :show, :id => @post.id
else
render 'edit'
end
end
</ruby>

The new method +update_attributes+ is used when you want to update a record
that already exists, and it accepts an hash containing the attributes
that you want to update. As before, if there was an error updating the
post we want to show the form back to the user.

TIP: you don't need to pass all attributes to +update_attributes+. For
example, if you'd call +@post.update_attributes(:title => 'A new title')
Rails would only update the +title+ attribute, leaving all other
attributes untouched.

Finally, we want to show a link to the +edit+ action in the +index+ and
+show+ views:

<erb>
# app/view/posts/index.html.erb

<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
</tr>

<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', :action => :show, :id => post.id %></td>
<td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
</tr>
<% end %>
</table>

# app/view/posts/show.html.erb

...

<%= link_to 'Back', :action => :index %>
| <%= link_to 'Edit', :action => :edit, :id => @post.id %>
</erb>

h4. Using the Console

To see your validations in action, you can use the console. The console is a
Expand Down

0 comments on commit f0de717

Please sign in to comment.