Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2e2afc0a
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,发现更多精彩内容 >>
提交
2e2afc0a
编写于
4月 20, 2012
作者:
O
Oscar Del Ben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add show action in getting started guide
上级
dbb4c4dd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
63 addition
and
27 deletion
+63
-27
guides/assets/images/getting_started/show_action_for_posts.png
...s/assets/images/getting_started/show_action_for_posts.png
+0
-0
guides/code/getting_started/app/controllers/posts_controller.rb
.../code/getting_started/app/controllers/posts_controller.rb
+6
-1
guides/code/getting_started/app/models/post.rb
guides/code/getting_started/app/models/post.rb
+0
-3
guides/code/getting_started/app/views/posts/show.html.erb
guides/code/getting_started/app/views/posts/show.html.erb
+4
-21
guides/code/getting_started/config/routes.rb
guides/code/getting_started/config/routes.rb
+1
-0
guides/source/getting_started.textile
guides/source/getting_started.textile
+52
-2
未找到文件。
guides/assets/images/getting_started/show_action_for_posts.png
0 → 100644
浏览文件 @
2e2afc0a
6.7 KB
guides/code/getting_started/app/controllers/posts_controller.rb
浏览文件 @
2e2afc0a
class
PostsController
<
ApplicationController
def
show
@post
=
Post
.
find
(
params
[
:id
])
end
def
new
end
...
...
@@ -7,6 +12,6 @@ def create
@post
=
Post
.
new
(
params
[
:post
])
@post
.
save
redirect_to
:action
=>
:
index
redirect_to
:action
=>
:
show
,
:id
=>
@post
.
id
end
end
guides/code/getting_started/app/models/post.rb
浏览文件 @
2e2afc0a
...
...
@@ -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
guides/code/getting_started/app/views/posts/show.html.erb
浏览文件 @
2e2afc0a
<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
.
conten
t
%>
<
strong>
Text:
</strong
>
<%=
@post
.
tex
t
%>
</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
%>
|
guides/code/getting_started/config/routes.rb
浏览文件 @
2e2afc0a
...
...
@@ -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.
...
...
guides/source/getting_started.textile
浏览文件 @
2e2afc0a
...
...
@@ -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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录