From ee4e7125a61c703332a8d591a0aee917ba828e62 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 25 Apr 2012 15:43:10 +0200 Subject: Add partials section to getting started guide --- .../getting_started/undefined_method_post_path.png | Bin 0 -> 15254 bytes .../getting_started/app/views/posts/_form.html.erb | 2 +- guides/code/getting_started/config/routes.rb | 4 +- guides/source/getting_started.textile | 102 ++++++++++++++++++++- 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 guides/assets/images/getting_started/undefined_method_post_path.png diff --git a/guides/assets/images/getting_started/undefined_method_post_path.png b/guides/assets/images/getting_started/undefined_method_post_path.png new file mode 100644 index 0000000000..f568bf315c Binary files /dev/null and b/guides/assets/images/getting_started/undefined_method_post_path.png differ 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 46ec257b91..b35ea2f237 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 => :update, :id => @post.id }, :method => :put do |f| %> +<%= form_for @post do |f| %> <% if @post.errors.any? %>

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

diff --git a/guides/code/getting_started/config/routes.rb b/guides/code/getting_started/config/routes.rb index b0973c62d5..02ca24e95a 100644 --- a/guides/code/getting_started/config/routes.rb +++ b/guides/code/getting_started/config/routes.rb @@ -6,9 +6,9 @@ Blog::Application.routes.draw do get "posts" => "posts#index" get "posts/new" post "posts/create" - get "posts/:id" => "posts#show" + get "posts/:id" => "posts#show", :as => :post get "posts/:id/edit" => "posts#edit" - put "posts/:id/update" => "posts#update" + put "posts/:id" => "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 88d5ce6d9b..fae12f825d 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -835,7 +835,7 @@ Moving on, we need to add the +update+ action. The file +config/routes.rb+ will need just one more line: -put "posts/:id/update" +put "posts/:id" => "posts#update" And the +update+ action in +posts_controller+ itself should not look too complicated by now: @@ -930,6 +930,106 @@ Our +edit+ action looks very similar to the +new+ action, in fact they both share the same code for displaying the form. Lets clean them up by using a +_form+ partial. +Create a new file +app/views/posts/_form.html.erb+ with the following +content: + + +<%= form_for @post 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 %> +
+ +Everything except for the +form_for+ declaration remained the same. I'll +explain later how +form_for+ can figure out the right +action+ and ++method+ attributes when building the form, for now let's update the ++new+ and +edit+ views: + + +# app/views/posts/new.html.erb + +

New post

+ +<%= render 'form' %> + +<%= link_to 'Back', :action => :index %> + + +# app/views/posts/edit.html.erb + +

Edit post

+ +<%= render 'form' %> + +<%= link_to 'Back', :action => :index %> +
+ +Point your browser to +"http://localhost:3000/posts/new":http://localhost:3000/posts/new and +try creating a new post. Everything still works. Now try editing the +post and you'll receive the following error: + +!images/getting_started/undefined_method_post_path.png(Undefined method +post_path)! + +To understand this error, you need to understand how +form_for+ works. +When you pass an object to +form_for+ and you don't specify a +:url+ +option, Rails will try to guess the +action+ and +method+ options by +checking if the passed object is a new record or not. Rails follows the +REST convention, so to create a new +Post+ object it will look for a +route named +posts_path+, and to update a +Post+ object it will look for +a route named +post_path+ and pass the current object. Similarly, rails +knows that it should create new objects via POST and update them via +PUT. + +If you run +rake routes+ from the console you'll see that we already +have a +posts_path+ route, which was created automatically by Rails. +However, we don't have a +post_path+ yet, which is the reason why we +received an error before. + + +# rake routes + + posts GET /posts(.:format) posts#index + posts_new GET /posts/new(.:format) posts#new +posts_create POST /posts/create(.:format) posts#create + GET /posts/:id(.:format) posts#show + GET /posts/:id/edit(.:format) posts#edit + PUT /posts/:id(.:format) posts#update + root / welcome#index + + +To fix this, open +config/routes.rb+ and modify the +get "posts/:id"+ +line like this: + + +get "posts/:id" => "posts#show", :as => :post + + +Now you'll be able to update posts again. + h4. Using the Console To see your validations in action, you can use the console. The console is a -- cgit v1.2.3