aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source
diff options
context:
space:
mode:
authorOscar Del Ben <info@oscardelben.com>2012-04-20 12:06:47 +0200
committerOscar Del Ben <info@oscardelben.com>2012-04-20 12:06:47 +0200
commit2e2afc0ac64190261df4b05428afddea96c8628c (patch)
treea94a70200a1ee434d3cfaa82e0040c6166d4692a /guides/source
parentdbb4c4ddb601e06fef37f63d644a5297f3292c2a (diff)
downloadrails-2e2afc0ac64190261df4b05428afddea96c8628c.tar.gz
rails-2e2afc0ac64190261df4b05428afddea96c8628c.tar.bz2
rails-2e2afc0ac64190261df4b05428afddea96c8628c.zip
Add show action in getting started guide
Diffstat (limited to 'guides/source')
-rw-r--r--guides/source/getting_started.textile54
1 files changed, 52 insertions, 2 deletions
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
</ruby>
@@ -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