From f0de7172a6e1a52bf122578954d805a36a488d27 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Tue, 24 Apr 2012 14:21:07 +0200 Subject: [PATCH] Add update post section to getting started guide --- .../app/controllers/posts_controller.rb | 14 ++ .../app/views/posts/_form.html.erb | 2 +- .../app/views/posts/edit.html.erb | 3 +- .../app/views/posts/index.html.erb | 2 + .../app/views/posts/show.html.erb | 1 + guides/code/getting_started/config/routes.rb | 2 + guides/source/getting_started.textile | 132 +++++++++++++++++- 7 files changed, 152 insertions(+), 4 deletions(-) diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb index 947cd2a767c87..fc71e9b4e8219 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 @@ 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 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 18cb29f335645..46ec257b91d1b 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? %>

<%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:

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 720580236baaa..911a48569d206 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 455a74b17fb53..3ba7091c15d01 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 @@ Title Text + <% @posts.each do |post| %> @@ -14,6 +15,7 @@ <%= post.title %> <%= post.text %> <%= link_to 'Show', :action => :show, :id => post.id %> + <%= link_to 'Edit', :action => :edit, :id => post.id %> <% end %> 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 a79fadfe4c8c4..aea28cd5a20f3 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 @@

<%= 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 10009a35cf8a3..b0973c62d5cf1 100644 --- a/guides/code/getting_started/config/routes.rb +++ b/guides/code/getting_started/config/routes.rb @@ -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. diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 73f85ab8f9c59..478aa74f27b7f 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -726,7 +726,7 @@ something went wrong. To do that, you'll modify +app/views/posts/index.html.erb+ to check for error messages: -<%= form_for :post do |f| %> +<%= form_for :post, :url => { :action => :create } do |f| %> <% if @post.errors.any? %>

<%= pluralize(@post.errors.count, "error") %> prohibited @@ -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+: + + +get "posts/:id/edit" => "posts#edit" + + +And then add the controller action: + + +def edit + @post = Post.find(params[:id]) +end + + +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: + + +

Editing post

+ +<%= form_for :post, :url => { :action => :update, :id => @post.id }, +:method => :put do |f| %> + <% if @post.errors.any? %> +
+

<%= pluralize(@post.errors.count, "error") %> prohibited + this post from being saved:

+
    + <% @post.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+ <% end %> +

+ <%= f.label :title %>
+ <%= f.text_field :title %> +

+ +

+ <%= f.label :text %>
+ <%= f.text_area :text %> +

+ +

+ <%= f.submit %> +

+<% end %> + +<%= link_to 'Back', :action => :index %> +
+ +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: + + +put "posts/:id/update" + + +And the +update+ action in +posts_controller+ itself should not look too complicated by now: + + +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 + + +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: + + +# app/view/posts/index.html.erb + + + + + + + + + +<% @posts.each do |post| %> + + + + + + +<% end %> +
TitleText
<%= post.title %><%= post.text %><%= link_to 'Show', :action => :show, :id => post.id %><%= link_to 'Edit', :action => :edit, :id => post.id %>
+ +# app/view/posts/show.html.erb + +... + +<%= link_to 'Back', :action => :index %> +| <%= link_to 'Edit', :action => :edit, :id => @post.id %> +
+ h4. Using the Console To see your validations in action, you can use the console. The console is a