Skip to content

Commit

Permalink
Add show action in getting started guide
Browse files Browse the repository at this point in the history
  • Loading branch information
oscardelben committed Apr 20, 2012
1 parent dbb4c4d commit 2e2afc0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -1,12 +1,17 @@
class PostsController < ApplicationController


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

def new
end

def create
@post = Post.new(params[:post])

@post.save
redirect_to :action => :index
redirect_to :action => :show, :id => @post.id
end
end
3 changes: 0 additions & 3 deletions guides/code/getting_started/app/models/post.rb
Expand Up @@ -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
25 changes: 4 additions & 21 deletions guides/code/getting_started/app/views/posts/show.html.erb
@@ -1,26 +1,9 @@
<p class="notice"><%= notice %></p>

<p>
<b>Name:</b>
<%= @post.name %>
</p>

<p>
<b>Title:</b>
<strong>Title:</strong>
<%= @post.title %>
</p>

<p>
<b>Content:</b>
<%= @post.content %>
<strong>Text:</strong>
<%= @post.text %>
</p>

<h2>Comments</h2>
<%= render @post.comments %>

<h2>Add a comment:</h2>
<%= render "comments/form" %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
1 change: 1 addition & 0 deletions guides/code/getting_started/config/routes.rb
Expand Up @@ -5,6 +5,7 @@

get "posts/new"
post "posts/create"
get "posts/:id" => "posts#show"

# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down
54 changes: 52 additions & 2 deletions guides/source/getting_started.textile
Expand Up @@ -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
</ruby>

Expand All @@ -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:

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

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.

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

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:

<erb>
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>

<p>
<strong>Text:</strong>
<%= @post.text %>
</p>
</erb>

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
Expand Down

0 comments on commit 2e2afc0

Please sign in to comment.