Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
06a1519a
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,体验更适合开发者的 AI 搜索 >>
提交
06a1519a
编写于
4月 30, 2012
作者:
O
Oscar Del Ben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite adding a second model section of Getting Started Guide
上级
bb6bd01d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
42 addition
and
39 deletion
+42
-39
guides/code/getting_started/app/views/posts/_form.html.erb
guides/code/getting_started/app/views/posts/_form.html.erb
+2
-2
guides/code/getting_started/app/views/posts/show.html.erb
guides/code/getting_started/app/views/posts/show.html.erb
+18
-2
guides/code/getting_started/config/routes.rb
guides/code/getting_started/config/routes.rb
+3
-1
guides/source/getting_started.textile
guides/source/getting_started.textile
+19
-34
未找到文件。
guides/code/getting_started/app/views/posts/_form.html.erb
浏览文件 @
06a1519a
...
...
@@ -10,12 +10,12 @@
</div>
<%
end
%>
<p>
<%=
f
.
label
:title
%>
<br>
<%=
f
.
label
:title
%>
<br
/
>
<%=
f
.
text_field
:title
%>
</p>
<p>
<%=
f
.
label
:text
%>
<br>
<%=
f
.
label
:text
%>
<br
/
>
<%=
f
.
text_area
:text
%>
</p>
...
...
guides/code/getting_started/app/views/posts/show.html.erb
浏览文件 @
06a1519a
...
...
@@ -8,5 +8,21 @@
<%=
@post
.
text
%>
</p>
<%=
link_to
'Back'
,
:action
=>
:index
%>
|
<%=
link_to
'Edit'
,
:action
=>
:edit
,
:id
=>
@post
.
id
%>
<h2>
Add a comment:
</h2>
<%=
form_for
([
@post
,
@post
.
comments
.
build
])
do
|
f
|
%>
<p>
<%=
f
.
label
:commenter
%>
<br
/>
<%=
f
.
text_field
:commenter
%>
</p>
<p>
<%=
f
.
label
:body
%>
<br
/>
<%=
f
.
text_area
:body
%>
</p>
<p>
<%=
f
.
submit
%>
</p>
<%
end
%>
<%=
link_to
'Edit Post'
,
edit_post_path
(
@post
)
%>
|
<%=
link_to
'Back to Posts'
,
posts_path
%>
guides/code/getting_started/config/routes.rb
浏览文件 @
06a1519a
Blog
::
Application
.
routes
.
draw
do
resources
:posts
resources
:posts
do
resources
:comments
end
# The priority is based upon order of creation:
# first created -> highest priority.
...
...
guides/source/getting_started.textile
浏览文件 @
06a1519a
...
...
@@ -394,11 +394,10 @@ This action is now displaying the parameters for the post that are coming in fro
h4. Creating the Post model
Rails uses models to manage database objects, so if you want to save
data to the database you'll have to create a model. In our blog
application you want to save posts, so you'll create a +Post+ model.
You can create a model with the following command:
Models in Rails use a singular name, and their corresponding database tables use
a plural name. Rails provides a generator for creating models, which
most Rails developers tend to use when creating new models.
To create the new model, run this command in your terminal:
<shell>
$ rails generate model Post title:string text:text
...
...
@@ -1155,18 +1154,14 @@ of declaring routes manually. For more information about routing, see
h3. Adding a Second Model
Now that you've seen what a model built with scaffolding looks like, it's time to
add a second model to the application. The second model will handle comments on
It's time to add a second model to the application. The second model will handle comments on
blog posts.
h4. Generating a Model
Models in Rails use a singular name, and their corresponding database tables use
a plural name. For the model to hold comments, the convention is to use the name
+Comment+. Even if you don't want to use the entire apparatus set up by
scaffolding, most Rails developers still use generators to make things like
models and controllers. To create the new model, run this command in your
terminal:
We're going to se the same generator that we used before when creating
the +Post+ model. This time we'll create a +Comment+ model to hold
reference of post comments. Run this command in your terminal:
<shell>
$ rails generate model Comment commenter:string body:text post:references
...
...
@@ -1254,7 +1249,6 @@ You'll need to edit the +post.rb+ file to add the other side of the association:
<ruby>
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
...
...
@@ -1273,9 +1267,7 @@ h4. Adding a Route for Comments
As with the +welcome+ controller, we will need to add a route so that Rails knows
where we would like to navigate to see +comments+. Open up the
+config/routes.rb+ file again. Near the top, you will see the entry for +posts+
that was added automatically by the scaffold generator: +resources
:posts+. Edit it as follows:
+config/routes.rb+ file again, and edit it as follows:
<ruby>
resources :posts do
...
...
@@ -1293,7 +1285,7 @@ In":routing.html guide.
h4. Generating a Controller
With the model in hand, you can turn your attention to creating a matching
controller. Again,
there's a generator for this
:
controller. Again,
we'll use the same generator we used before
:
<shell>
$ rails generate controller Comments
...
...
@@ -1320,36 +1312,29 @@ So first, we'll wire up the Post show template
(+/app/views/posts/show.html.erb+) to let us make a new comment:
<erb>
<p id="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.
content
%>
<
strong>Text:</strong
>
<%= @post.
texthttp://beginningruby.org/
%>
</p>
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<
div class="field"
>
<
p
>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</
div
>
<
div class="field"
>
</
p
>
<
p
>
<%= f.label :body %><br />
<%= f.text_area :body %>
</
div
>
<
div class="actions"
>
</
p
>
<
p
>
<%= f.submit %>
</
div
>
</
p
>
<% end %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录