Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ee4e7125
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,发现更多精彩内容 >>
提交
ee4e7125
编写于
4月 25, 2012
作者:
O
Oscar Del Ben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add partials section to getting started guide
上级
ccbd32c2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
104 addition
and
4 deletion
+104
-4
guides/assets/images/getting_started/undefined_method_post_path.png
...ets/images/getting_started/undefined_method_post_path.png
+0
-0
guides/code/getting_started/app/views/posts/_form.html.erb
guides/code/getting_started/app/views/posts/_form.html.erb
+1
-1
guides/code/getting_started/config/routes.rb
guides/code/getting_started/config/routes.rb
+2
-2
guides/source/getting_started.textile
guides/source/getting_started.textile
+101
-1
未找到文件。
guides/assets/images/getting_started/undefined_method_post_path.png
0 → 100644
浏览文件 @
ee4e7125
14.9 KB
guides/code/getting_started/app/views/posts/_form.html.erb
浏览文件 @
ee4e7125
<%=
form_for
:post
,
:url
=>
{
:action
=>
:update
,
:id
=>
@post
.
id
},
:method
=>
:pu
t
do
|
f
|
%>
<%=
form_for
@pos
t
do
|
f
|
%>
<%
if
@post
.
errors
.
any?
%>
<div
id=
"errorExplanation"
>
<h2>
<%=
pluralize
(
@post
.
errors
.
count
,
"error"
)
%>
prohibited this post from being saved:
</h2>
...
...
guides/code/getting_started/config/routes.rb
浏览文件 @
ee4e7125
...
...
@@ -6,9 +6,9 @@
get
"posts"
=>
"posts#index"
get
"posts/new"
post
"posts/create"
get
"posts/:id"
=>
"posts#show"
get
"posts/:id"
=>
"posts#show"
,
:as
=>
:post
get
"posts/:id/edit"
=>
"posts#edit"
put
"posts/:id
/update
"
=>
"posts#update"
put
"posts/:id"
=>
"posts#update"
# The priority is based upon order of creation:
# first created -> highest priority.
...
...
guides/source/getting_started.textile
浏览文件 @
ee4e7125
...
...
@@ -835,7 +835,7 @@ Moving on, we need to add the +update+ action. The file
+config/routes.rb+ will need just one more line:
<ruby>
put "posts/:id
/
update"
put "posts/:id
" => "posts#
update"
</ruby>
And the +update+ action in +posts_controller+ itself should not look too complicated by now:
...
...
@@ -930,6 +930,106 @@ Our +edit+ action looks very similar to the +new+ action, in fact they
both share the same code for displaying the form. Lets clean them up by
using a +_form+ partial.
Create a new file +app/views/posts/_form.html.erb+ with the following
content:
<erb>
<
%=
form_for
@
post
do
|
f
|
%
>
<
%
if
@
post.errors.any
?
%
>
<div
id=
"errorExplanation"
>
<h2><
%=
pluralize
(@
post.errors.count
,
"
error
")
%
>
prohibited
this post from being saved:
</h2>
<ul>
<
%
@
post.errors.full_messages.each
do
|
msg
|
%
>
<li><
%=
msg
%
></li>
<
%
end
%
>
</ul>
</div>
<
%
end
%
>
<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
%
>
</erb>
Everything except for the +form_for+ declaration remained the same. I'll
explain later how +form_for+ can figure out the right +action+ and
+method+ attributes when building the form, for now let's update the
+new+ and +edit+ views:
<erb>
# app/views/posts/new.html.erb
<h1>
New post
</h1>
<
%=
render
'
form
'
%
>
<
%=
link_to
'
Back
',
:action =
>
:index
%
>
# app/views/posts/edit.html.erb
<h1>
Edit post
</h1>
<
%=
render
'
form
'
%
>
<
%=
link_to
'
Back
',
:action =
>
:index
%
>
</erb>
Point your browser to
"http://localhost:3000/posts/new":http://localhost:3000/posts/new and
try creating a new post. Everything still works. Now try editing the
post and you'll receive the following error:
!images/getting_started/undefined_method_post_path.png(Undefined method
post_path)!
To understand this error, you need to understand how +form_for+ works.
When you pass an object to +form_for+ and you don't specify a +:url+
option, Rails will try to guess the +action+ and +method+ options by
checking if the passed object is a new record or not. Rails follows the
REST convention, so to create a new +Post+ object it will look for a
route named +posts_path+, and to update a +Post+ object it will look for
a route named +post_path+ and pass the current object. Similarly, rails
knows that it should create new objects via POST and update them via
PUT.
If you run +rake routes+ from the console you'll see that we already
have a +posts_path+ route, which was created automatically by Rails.
However, we don't have a +post_path+ yet, which is the reason why we
received an error before.
<shell>
# rake routes
posts GET /posts(.:format) posts#index
posts_new GET /posts/new(.:format) posts#new
posts_create POST /posts/create(.:format) posts#create
GET /posts/:id(.:format) posts#show
GET /posts/:id/edit(.:format) posts#edit
PUT /posts/:id(.:format) posts#update
root / welcome#index
</shell>
To fix this, open +config/routes.rb+ and modify the +get "posts/:id"+
line like this:
<ruby>
get "posts/:id" => "posts#show", :as => :post
</ruby>
Now you'll be able to update posts again.
h4. Using the Console
To see your validations in action, you can use the console. The console is a
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录