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
---
.../app/controllers/home_controller.rb | 2 +-
.../app/controllers/posts_controller.rb | 3 +
.../getting_started/app/helpers/home_helper.rb | 2 +-
.../getting_started/app/views/home/index.html.erb | 2 -
.../getting_started/app/views/posts/index.html.erb | 18 +---
.../getting_started/app/views/posts/new.html.erb | 2 +-
.../getting_started/app/views/posts/show.html.erb | 2 +
.../app/views/welcome/index.html.erb | 2 +
guides/code/getting_started/config/routes.rb | 3 +-
.../test/functional/home_controller_test.rb | 2 +-
guides/source/getting_started.textile | 109 ++++++++++++++++++++-
11 files changed, 123 insertions(+), 24 deletions(-)
delete mode 100644 guides/code/getting_started/app/views/home/index.html.erb
create mode 100644 guides/code/getting_started/app/views/welcome/index.html.erb
(limited to 'guides')
diff --git a/guides/code/getting_started/app/controllers/home_controller.rb b/guides/code/getting_started/app/controllers/home_controller.rb
index 6cc31c1ca3..309b70441e 100644
--- a/guides/code/getting_started/app/controllers/home_controller.rb
+++ b/guides/code/getting_started/app/controllers/home_controller.rb
@@ -1,4 +1,4 @@
-class HomeController < ApplicationController
+class WelcomeController < ApplicationController
def index
end
diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb
index f9181f98c6..e4d83dd279 100644
--- a/guides/code/getting_started/app/controllers/posts_controller.rb
+++ b/guides/code/getting_started/app/controllers/posts_controller.rb
@@ -1,5 +1,8 @@
class PostsController < ApplicationController
+ def index
+ @posts = Post.all
+ end
def show
@post = Post.find(params[:id])
diff --git a/guides/code/getting_started/app/helpers/home_helper.rb b/guides/code/getting_started/app/helpers/home_helper.rb
index 23de56ac60..eeead45fc9 100644
--- a/guides/code/getting_started/app/helpers/home_helper.rb
+++ b/guides/code/getting_started/app/helpers/home_helper.rb
@@ -1,2 +1,2 @@
-module HomeHelper
+module WelcomeHelper
end
diff --git a/guides/code/getting_started/app/views/home/index.html.erb b/guides/code/getting_started/app/views/home/index.html.erb
deleted file mode 100644
index bb4f3dcd1f..0000000000
--- a/guides/code/getting_started/app/views/home/index.html.erb
+++ /dev/null
@@ -1,2 +0,0 @@
-Hello, Rails!
-<%= link_to "My Blog", posts_path %>
diff --git a/guides/code/getting_started/app/views/posts/index.html.erb b/guides/code/getting_started/app/views/posts/index.html.erb
index 45dee1b25f..455a74b17f 100644
--- a/guides/code/getting_started/app/views/posts/index.html.erb
+++ b/guides/code/getting_started/app/views/posts/index.html.erb
@@ -1,27 +1,19 @@
Listing posts
+<%= link_to 'New post', :action => :new %>
+
- Name |
Title |
- Content |
- |
- |
+ Text |
|
<% @posts.each do |post| %>
- <%= post.name %> |
<%= post.title %> |
- <%= post.content %> |
- <%= link_to 'Show', post %> |
- <%= link_to 'Edit', edit_post_path(post) %> |
- <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %> |
+ <%= post.text %> |
+ <%= link_to 'Show', :action => :show, :id => post.id %>
|
<% end %>
-
-
-
-<%= link_to 'New Post', new_post_path %>
diff --git a/guides/code/getting_started/app/views/posts/new.html.erb b/guides/code/getting_started/app/views/posts/new.html.erb
index 5d6482f880..ce9523a721 100644
--- a/guides/code/getting_started/app/views/posts/new.html.erb
+++ b/guides/code/getting_started/app/views/posts/new.html.erb
@@ -2,4 +2,4 @@
<%= render 'form' %>
-<%#= link_to 'Back', posts_path %>
+<%= link_to 'Back', :action => :index %>
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 6207babdf0..a79fadfe4c 100644
--- a/guides/code/getting_started/app/views/posts/show.html.erb
+++ b/guides/code/getting_started/app/views/posts/show.html.erb
@@ -7,3 +7,5 @@
Text:
<%= @post.text %>
+
+<%= link_to 'Back', :action => :index %>
diff --git a/guides/code/getting_started/app/views/welcome/index.html.erb b/guides/code/getting_started/app/views/welcome/index.html.erb
new file mode 100644
index 0000000000..e04680ea7e
--- /dev/null
+++ b/guides/code/getting_started/app/views/welcome/index.html.erb
@@ -0,0 +1,2 @@
+Hello, Rails!
+<%= link_to "My Blog", :controller => "posts" %>
diff --git a/guides/code/getting_started/config/routes.rb b/guides/code/getting_started/config/routes.rb
index 7210b559bb..10009a35cf 100644
--- a/guides/code/getting_started/config/routes.rb
+++ b/guides/code/getting_started/config/routes.rb
@@ -3,6 +3,7 @@ Blog::Application.routes.draw do
# resources :comments
# end
+ get "posts" => "posts#index"
get "posts/new"
post "posts/create"
get "posts/:id" => "posts#show"
@@ -56,7 +57,7 @@ Blog::Application.routes.draw do
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- root :to => "home#index"
+ root :to => "welcome#index"
# See how all your routes lay out with "rake routes"
diff --git a/guides/code/getting_started/test/functional/home_controller_test.rb b/guides/code/getting_started/test/functional/home_controller_test.rb
index 0d9bb47c3e..dff8e9d2c5 100644
--- a/guides/code/getting_started/test/functional/home_controller_test.rb
+++ b/guides/code/getting_started/test/functional/home_controller_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'
-class HomeControllerTest < ActionController::TestCase
+class WelcomeControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
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
+
+
+
+ Title |
+ Text |
+
+
+<% @posts.each do |post| %>
+
+ <%= post.title %> |
+ <%= post.text %> |
+
+<% end %>
+
+
+
+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 %>
+
+
+
+ Title |
+ Text |
+ |
+
+
+<% @posts.each do |post| %>
+
+ <%= post.title %> |
+ <%= post.text %> |
+ <%= link_to 'Show', :action => :show, :id => post.id %> |
+
+<% end %>
+
+
+# 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