Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e7e72aa2
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e7e72aa2
编写于
4月 20, 2012
作者:
O
Oscar Del Ben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add index and links section to Getting started guide
上级
2e2afc0a
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
123 addition
and
22 deletion
+123
-22
guides/code/getting_started/app/controllers/home_controller.rb
...s/code/getting_started/app/controllers/home_controller.rb
+1
-1
guides/code/getting_started/app/controllers/posts_controller.rb
.../code/getting_started/app/controllers/posts_controller.rb
+3
-0
guides/code/getting_started/app/helpers/home_helper.rb
guides/code/getting_started/app/helpers/home_helper.rb
+1
-1
guides/code/getting_started/app/views/posts/index.html.erb
guides/code/getting_started/app/views/posts/index.html.erb
+5
-13
guides/code/getting_started/app/views/posts/new.html.erb
guides/code/getting_started/app/views/posts/new.html.erb
+1
-1
guides/code/getting_started/app/views/posts/show.html.erb
guides/code/getting_started/app/views/posts/show.html.erb
+2
-0
guides/code/getting_started/app/views/welcome/index.html.erb
guides/code/getting_started/app/views/welcome/index.html.erb
+2
-0
guides/code/getting_started/config/routes.rb
guides/code/getting_started/config/routes.rb
+2
-1
guides/code/getting_started/test/functional/home_controller_test.rb
...e/getting_started/test/functional/home_controller_test.rb
+1
-1
guides/source/getting_started.textile
guides/source/getting_started.textile
+105
-4
未找到文件。
guides/code/getting_started/app/controllers/home_controller.rb
浏览文件 @
e7e72aa2
class
H
omeController
<
ApplicationController
class
Welc
omeController
<
ApplicationController
def
index
end
...
...
guides/code/getting_started/app/controllers/posts_controller.rb
浏览文件 @
e7e72aa2
class
PostsController
<
ApplicationController
def
index
@posts
=
Post
.
all
end
def
show
@post
=
Post
.
find
(
params
[
:id
])
...
...
guides/code/getting_started/app/helpers/home_helper.rb
浏览文件 @
e7e72aa2
module
H
omeHelper
module
Welc
omeHelper
end
guides/code/getting_started/app/views/posts/index.html.erb
浏览文件 @
e7e72aa2
<h1>
Listing posts
</h1>
<%=
link_to
'New post'
,
:action
=>
:new
%>
<table>
<tr>
<th>
Name
</th>
<th>
Title
</th>
<th>
Content
</th>
<th></th>
<th></th>
<th>
Text
</th>
<th></th>
</tr>
<%
@posts
.
each
do
|
post
|
%>
<tr>
<td>
<%=
post
.
name
%>
</td>
<td>
<%=
post
.
title
%>
</td>
<td>
<%=
post
.
content
%>
</td>
<td>
<%=
link_to
'Show'
,
post
%>
</td>
<td>
<%=
link_to
'Edit'
,
edit_post_path
(
post
)
%>
</td>
<td>
<%=
link_to
'Destroy'
,
post
,
confirm:
'Are you sure?'
,
method: :delete
%>
</td>
<td>
<%=
post
.
text
%>
</td>
<td>
<%=
link_to
'Show'
,
:action
=>
:show
,
:id
=>
post
.
id
%>
</tr>
<%
end
%>
</table>
<br
/>
<%=
link_to
'New Post'
,
new_post_path
%>
guides/code/getting_started/app/views/posts/new.html.erb
浏览文件 @
e7e72aa2
...
...
@@ -2,4 +2,4 @@
<%=
render
'form'
%>
<%
#= link_to 'Back', posts_path
%>
<%
=
link_to
'Back'
,
:action
=>
:index
%>
guides/code/getting_started/app/views/posts/show.html.erb
浏览文件 @
e7e72aa2
...
...
@@ -7,3 +7,5 @@
<strong>
Text:
</strong>
<%=
@post
.
text
%>
</p>
<%=
link_to
'Back'
,
:action
=>
:index
%>
guides/code/getting_started/app/views/
h
ome/index.html.erb
→
guides/code/getting_started/app/views/
welc
ome/index.html.erb
浏览文件 @
e7e72aa2
<h1>
Hello, Rails!
</h1>
<%=
link_to
"My Blog"
,
posts_path
%>
<%=
link_to
"My Blog"
,
:controller
=>
"posts"
%>
guides/code/getting_started/config/routes.rb
浏览文件 @
e7e72aa2
...
...
@@ -3,6 +3,7 @@
# resources :comments
# end
get
"posts"
=>
"posts#index"
get
"posts/new"
post
"posts/create"
get
"posts/:id"
=>
"posts#show"
...
...
@@ -56,7 +57,7 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root
:to
=>
"
h
ome#index"
root
:to
=>
"
welc
ome#index"
# See how all your routes lay out with "rake routes"
...
...
guides/code/getting_started/test/functional/home_controller_test.rb
浏览文件 @
e7e72aa2
require
'test_helper'
class
H
omeControllerTest
<
ActionController
::
TestCase
class
Welc
omeControllerTest
<
ActionController
::
TestCase
test
"should get index"
do
get
:index
assert_response
:success
...
...
guides/source/getting_started.textile
浏览文件 @
e7e72aa2
...
...
@@ -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:
<ruby>
# Add to config/routes.rb
get "posts" => "posts#index"
# Add to app/controllers/posts_controller.rb
def index
@posts = Post.all
end
</ruby>
+app/view/posts/index.html.erb+:
<erb>
<h1>
Listing posts
</h1>
<table>
<tr>
<th>
Title
</th>
<th>
Text
</th>
</tr>
<
%
@
posts.each
do
|
post
|
%
>
<tr>
<td><
%=
post.title
%
></td>
<td><
%=
post.text
%
></td>
</tr>
<
%
end
%
>
</table>
</erb>
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:
<ruby>
<h1>
Hello, Rails!
</h1>
<
%=
link_to
"
My
Blog
",
posts_path
%
>
<
%=
link_to
"
My
Blog
",
:controller =
>
"posts"
%
>
</ruby>
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.
<erb>
# app/views/posts/index.html.erb
<h1>
Listing posts
</h1>
<
%=
link_to
'
New
post
',
:action =
>
:new
%
>
<table>
<tr>
<th>
Title
</th>
<th>
Text
</th>
<th></th>
</tr>
<
%
@
posts.each
do
|
post
|
%
>
<tr>
<td><
%=
post.title
%
></td>
<td><
%=
post.text
%
></td>
<td><
%=
link_to
'
Show
',
:action =
>
:show,
:id =
>
post.id
%
></td>
</tr>
<
%
end
%
>
</table>
# app/views/posts/new.html.erb
<
%=
form_for
:post
do
|
f
|
%
>
<p>
<
%=
f.label
:title
%
><br>
<
%=
f.text_field
:title
%
>
</p>
<p>
<
%=
f.label
:text
%
><br>
<
%=
f.text_area
:text
%
>
</p>
<p>
<
%=
f.submit
%
>
</p>
<
%
end
%
>
<
%=
link_to
'
Back
',
:action =
>
:index
%
>
# app/views/posts/show.html.erb
<p>
<strong>
Title:
</strong>
<
%=
@
post.title
%
>
</p>
<p>
<strong>
Text:
</strong>
<
%=
@
post.text
%
>
</p>
<
%=
link_to
'
Back
',
:action =
>
:index
%
>
</erb>
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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录