aboutsummaryrefslogtreecommitdiffstats
path: root/guides/code
diff options
context:
space:
mode:
authorOscar Del Ben <info@oscardelben.com>2012-04-24 14:21:07 +0200
committerOscar Del Ben <info@oscardelben.com>2012-04-24 14:21:07 +0200
commitf0de7172a6e1a52bf122578954d805a36a488d27 (patch)
treee5d5a1899f1d3d9224cc0245795c55dc8f62349f /guides/code
parent5acb345a86584e3c9c4b5a81c4499c767393c89a (diff)
downloadrails-f0de7172a6e1a52bf122578954d805a36a488d27.tar.gz
rails-f0de7172a6e1a52bf122578954d805a36a488d27.tar.bz2
rails-f0de7172a6e1a52bf122578954d805a36a488d27.zip
Add update post section to getting started guide
Diffstat (limited to 'guides/code')
-rw-r--r--guides/code/getting_started/app/controllers/posts_controller.rb14
-rw-r--r--guides/code/getting_started/app/views/posts/_form.html.erb2
-rw-r--r--guides/code/getting_started/app/views/posts/edit.html.erb3
-rw-r--r--guides/code/getting_started/app/views/posts/index.html.erb2
-rw-r--r--guides/code/getting_started/app/views/posts/show.html.erb1
-rw-r--r--guides/code/getting_started/config/routes.rb2
6 files changed, 21 insertions, 3 deletions
diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb
index 947cd2a767..fc71e9b4e8 100644
--- a/guides/code/getting_started/app/controllers/posts_controller.rb
+++ b/guides/code/getting_started/app/controllers/posts_controller.rb
@@ -21,4 +21,18 @@ class PostsController < ApplicationController
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
diff --git a/guides/code/getting_started/app/views/posts/_form.html.erb b/guides/code/getting_started/app/views/posts/_form.html.erb
index 18cb29f335..46ec257b91 100644
--- a/guides/code/getting_started/app/views/posts/_form.html.erb
+++ b/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>
diff --git a/guides/code/getting_started/app/views/posts/edit.html.erb b/guides/code/getting_started/app/views/posts/edit.html.erb
index 720580236b..911a48569d 100644
--- a/guides/code/getting_started/app/views/posts/edit.html.erb
+++ b/guides/code/getting_started/app/views/posts/edit.html.erb
@@ -2,5 +2,4 @@
<%= render 'form' %>
-<%= link_to 'Show', @post %> |
-<%= link_to 'Back', posts_path %>
+<%= link_to 'Back', :action => :index %>
diff --git a/guides/code/getting_started/app/views/posts/index.html.erb b/guides/code/getting_started/app/views/posts/index.html.erb
index 455a74b17f..3ba7091c15 100644
--- a/guides/code/getting_started/app/views/posts/index.html.erb
+++ b/guides/code/getting_started/app/views/posts/index.html.erb
@@ -7,6 +7,7 @@
<th>Title</th>
<th>Text</th>
<th></th>
+ <th></th>
</tr>
<% @posts.each do |post| %>
@@ -14,6 +15,7 @@
<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>
diff --git a/guides/code/getting_started/app/views/posts/show.html.erb b/guides/code/getting_started/app/views/posts/show.html.erb
index a79fadfe4c..aea28cd5a2 100644
--- a/guides/code/getting_started/app/views/posts/show.html.erb
+++ b/guides/code/getting_started/app/views/posts/show.html.erb
@@ -9,3 +9,4 @@
</p>
<%= link_to 'Back', :action => :index %>
+| <%= link_to 'Edit', :action => :edit, :id => @post.id %>
diff --git a/guides/code/getting_started/config/routes.rb b/guides/code/getting_started/config/routes.rb
index 10009a35cf..b0973c62d5 100644
--- a/guides/code/getting_started/config/routes.rb
+++ b/guides/code/getting_started/config/routes.rb
@@ -7,6 +7,8 @@ Blog::Application.routes.draw do
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.