From e7e72aa253d9cb3bef786a955794986d1f3ff871 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Fri, 20 Apr 2012 12:53:18 +0200 Subject: Add index and links section to Getting started guide --- guides/source/getting_started.textile | 109 ++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index b86f785e3d..9f1a7db36f 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -540,20 +540,121 @@ be able to create a post. Try it! !images/getting_started/show_action_for_posts.png(Show action for posts)! -h4. Adding a Link +h4. Listing all posts -To hook the posts up to the home page you've already created, you can add a link -to the home page. Open +app/views/welcome/index.html.erb+ and modify it as follows: +We still need a way to list all our posts, so let's do that. As usual, +we'll need a route, a controller action, and a view: + + +# Add to config/routes.rb +get "posts" => "posts#index" + +# Add to app/controllers/posts_controller.rb +def index + @posts = Post.all +end + + ++app/view/posts/index.html.erb+: + + +

Listing posts

+ + + + + + + +<% @posts.each do |post| %> + + + + +<% end %> +
TitleText
<%= post.title %><%= post.text %>
+
+ +h4. Adding links + +You can now create, show, and list posts. But it's difficult to navigate +through pages, so let's add some links. + +Open +app/views/welcome/index.html.erb+ and modify it as follows:

Hello, Rails!

-<%= link_to "My Blog", posts_path %> +<%= link_to "My Blog", :controller => "posts" %>
The +link_to+ method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for posts. +Let's add links to the other views as well. + + +# app/views/posts/index.html.erb + +

Listing posts

+ +<%= link_to 'New post', :action => :new %> + + + + + + + + +<% @posts.each do |post| %> + + + + + +<% end %> +
TitleText
<%= post.title %><%= post.text %><%= link_to 'Show', :action => :show, :id => post.id %>
+ +# app/views/posts/new.html.erb + +<%= form_for :post do |f| %> +

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

+ +

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

+ +

+ <%= f.submit %> +

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

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

+ +

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

+ +<%= link_to 'Back', :action => :index %> +
+ +TIP: If you want to link to an action in the same controller, you don't +need to specify the +:controller+ option, as Rails will use the current +controller by default. + h4. Working with Posts in the Browser Now you're ready to start working with posts. To do that, navigate to -- cgit v1.2.3