Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3da2b530
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,发现更多精彩内容 >>
提交
3da2b530
编写于
4月 21, 2012
作者:
O
Oscar Del Ben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add validation code to getting started guide and improve validation
section
上级
504ba12e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
24 addition
and
10 deletion
+24
-10
guides/assets/images/getting_started/form_with_errors.png
guides/assets/images/getting_started/form_with_errors.png
+0
-0
guides/code/getting_started/app/controllers/posts_controller.rb
.../code/getting_started/app/controllers/posts_controller.rb
+1
-0
guides/code/getting_started/app/views/posts/_form.html.erb
guides/code/getting_started/app/views/posts/_form.html.erb
+10
-0
guides/source/getting_started.textile
guides/source/getting_started.textile
+13
-10
未找到文件。
guides/assets/images/getting_started/form_with_errors.png
0 → 100644
浏览文件 @
3da2b530
20.3 KB
guides/code/getting_started/app/controllers/posts_controller.rb
浏览文件 @
3da2b530
...
...
@@ -9,6 +9,7 @@ def show
end
def
new
@post
=
Post
.
new
end
def
create
...
...
guides/code/getting_started/app/views/posts/_form.html.erb
浏览文件 @
3da2b530
<%=
form_for
:post
,
:url
=>
{
:action
=>
:create
}
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
%>
...
...
guides/source/getting_started.textile
浏览文件 @
3da2b530
...
...
@@ -695,9 +695,8 @@ in "Active Record Validations and
Callbacks":active_record_validations_callbacks.html#validations-overview
If you open +posts_controller+ again, you'll notice that we don't check
the result of calling +@post.save+, but now if we don't pass a valid
title, +save+ will return false and we need to show the form back to the
user. To do that, modify the +create+ action to look like the following:
the result of calling +@post.save+. We need to change its behavior to
show the form back to the user if any error occur:
<ruby>
def new
...
...
@@ -716,12 +715,12 @@ end
</ruby>
Notice that I've also added +@post = Post.new+ to the +new+ action. I'll
explain why I did that in the next section.
explain why I did that in the next section, for now add that to your
controller as well.
Now, if validations fail and +save+ returns false, we show the form back
to the user. Note that we use +render+ instead of +redirect_to+. We do
that because +render+ will pass the +@post+ variable back to the form,
which contains the error information that we need.
Also notice that we use +render+ instead of +redirect_to+ when +save+
returns false. We can use +render+ so that the +@post+ object is passed
back to the view.
If you reload
"http://localhost:3000/posts/new":http://localhost:3000/posts/new and
...
...
@@ -762,8 +761,10 @@ something went wrong. To do that, you'll modify
</erb>
A few things are going on. We check if there are any errors with
+@post.errors.any?+, and if that returns true we show the list of all
errors with +@post.errors.full_messages+. +pluralize+ is a rails helper
+@post.errors.any?+, and in that case we show a list of all
errors with +@post.errors.full_messages+.
+pluralize+ is a rails helper
that takes a number and a string as its arguments. If the number is
greater than one, the string will be automatically pluralized.
...
...
@@ -775,7 +776,9 @@ TIP: Rails automatically wraps fields that contain an error with a div
with class +field_with_errors+. You can define a css rule to make them
standout.
Now you'll get a nice error message when saving a post without title:
!images/getting_started/form_with_errors.png(Form With Errors)!
h4. Using the Console
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录