From 2e2afc0ac64190261df4b05428afddea96c8628c Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Fri, 20 Apr 2012 12:06:47 +0200 Subject: Add show action in getting started guide --- .../getting_started/show_action_for_posts.png | Bin 0 -> 6885 bytes .../app/controllers/posts_controller.rb | 7 ++- guides/code/getting_started/app/models/post.rb | 3 -- .../getting_started/app/views/posts/show.html.erb | 25 ++-------- guides/code/getting_started/config/routes.rb | 1 + guides/source/getting_started.textile | 54 ++++++++++++++++++++- 6 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 guides/assets/images/getting_started/show_action_for_posts.png (limited to 'guides') diff --git a/guides/assets/images/getting_started/show_action_for_posts.png b/guides/assets/images/getting_started/show_action_for_posts.png new file mode 100644 index 0000000000..5c8c4d8e5e Binary files /dev/null and b/guides/assets/images/getting_started/show_action_for_posts.png differ diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb index 3373443b16..f9181f98c6 100644 --- a/guides/code/getting_started/app/controllers/posts_controller.rb +++ b/guides/code/getting_started/app/controllers/posts_controller.rb @@ -1,5 +1,10 @@ class PostsController < ApplicationController + + def show + @post = Post.find(params[:id]) + end + def new end @@ -7,6 +12,6 @@ class PostsController < ApplicationController @post = Post.new(params[:post]) @post.save - redirect_to :action => :index + redirect_to :action => :show, :id => @post.id end end diff --git a/guides/code/getting_started/app/models/post.rb b/guides/code/getting_started/app/models/post.rb index e77e607f3f..21387340b0 100644 --- a/guides/code/getting_started/app/models/post.rb +++ b/guides/code/getting_started/app/models/post.rb @@ -3,7 +3,4 @@ class Post < ActiveRecord::Base :length => { :minimum => 5 } has_many :comments, :dependent => :destroy - - accepts_nested_attributes_for :tags, :allow_destroy => :true, - :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } 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 3445fd8779..6207babdf0 100644 --- a/guides/code/getting_started/app/views/posts/show.html.erb +++ b/guides/code/getting_started/app/views/posts/show.html.erb @@ -1,26 +1,9 @@ -

<%= notice %>

-

- Name: - <%= @post.name %> -

- -

- Title: + Title: <%= @post.title %>

- +

- Content: - <%= @post.content %> + Text: + <%= @post.text %>

- -

Comments

-<%= render @post.comments %> - -

Add a comment:

-<%= render "comments/form" %> - - -<%= link_to 'Edit Post', edit_post_path(@post) %> | -<%= link_to 'Back to Posts', posts_path %> | diff --git a/guides/code/getting_started/config/routes.rb b/guides/code/getting_started/config/routes.rb index 98b1a6dcc8..7210b559bb 100644 --- a/guides/code/getting_started/config/routes.rb +++ b/guides/code/getting_started/config/routes.rb @@ -5,6 +5,7 @@ Blog::Application.routes.draw do get "posts/new" post "posts/create" + get "posts/:id" => "posts#show" # 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 6de9cc39bc..b86f785e3d 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -474,7 +474,7 @@ def create @post = Post.new(params[:post]) @post.save - redirect_to :action => :index + redirect_to :action => :show, :id => @post.id end @@ -483,13 +483,63 @@ respective attributes, which are automatically mapped to its database columns. In the first line we do just that (remember that +params[:post]+ contains the attributes we're interested in). Then, +@post.save+ is responsible for saving the model in the database. -Finally, on the last line we redirect the user to the +index+ action, +Finally, on the last line we redirect the user to the +show+ action, wich we have not defined yet. TIP: As we'll see later, +@post.save+ returns a boolean indicating wherever the model was saved or not, and you can (and usually do) take different actions depending on the result of calling +@post.save+. +h4. Showing posts + +Before trying to create a new post, let's add the +show+ action, which +will be responsible for showing our posts. Open +config/routes.rb+ +and add the following route: + + +get "posts/:id" => "posts#show" + + +The special syntax +:id+ tells rails that this route expects an +:id+ +parameter, which in our case will be the id of the post. Note that this +time we had to specify the actual mapping, +posts#show+ because +otherwise Rails would not know which action to render. + +As we did before, we need to add the +show+ action in the ++posts_controller+ and its respective view. + + +def show + @post = Post.find(params[:id]) +end + + +A couple of things to note. We use +Post.find+ to find the post we're +interested in. We also use an instance variable (prefixed by +@+) to +hold our reference to the post object. We do this because Rails will pass all instance +variables to the view. + +Now, create a new file +app/view/posts/show.html.erb+ with the following +content: + + +

+ Title: + <%= @post.title %> +

+ +

+ Text: + <%= @post.text %> +

+
+ +Finally, if you now go to +"http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll +be able to create a post. Try it! + +!images/getting_started/show_action_for_posts.png(Show action for posts)! + h4. Adding a Link To hook the posts up to the home page you've already created, you can add a link -- cgit v1.2.3