Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1f80b8b0
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,发现更多精彩内容 >>
提交
1f80b8b0
编写于
4月 11, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of gitproxy:rails/rails
上级
5b89fd07
fb4b454d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
55 addition
and
13 deletion
+55
-13
actionpack/lib/action_view/helpers/form_helper.rb
actionpack/lib/action_view/helpers/form_helper.rb
+55
-13
未找到文件。
actionpack/lib/action_view/helpers/form_helper.rb
浏览文件 @
1f80b8b0
...
...
@@ -11,21 +11,25 @@ module Helpers
# Form helpers are designed to make working with resources much easier
# compared to using vanilla HTML.
#
#
Model-based form
s are created with +form_for+. That method yields a form
#
Forms for model
s are created with +form_for+. That method yields a form
# builder that knows the model the form is about. The form builder is thus
# able to generate default values for input fields that correspond to model
# attributes, and also convenient
element
names, IDs, endpoints, etc.
# attributes, and also convenient names, IDs, endpoints, etc.
#
# Conventions in the generated field names allow controllers to receive form
# data nicely structured in +params+ with no effort on your side.
#
# For example, to create a new
+Person+ resource you typically set up a new
#
instance in
<tt>PeopleController#new</tt> action, <tt>@person</tt>, and
#
write the form in <tt>new.html.erb</tt> this way
:
# For example, to create a new
person you typically set up a new instance of
#
+Person+ in the
<tt>PeopleController#new</tt> action, <tt>@person</tt>, and
#
pass it to +form_for+
:
#
# <%= form_for @person do |f| %>
# <%= f.text_field :first_name %>
# <%= f.text_field :last_name %>
# <%= f.label :first_name %>:
# <%= f.text_field :first_name %><br />
#
# <%= f.label :last_name %>:
# <%= f.text_field :last_name %><br />
#
# <%= f.submit %>
# <% end %>
#
...
...
@@ -35,16 +39,54 @@ module Helpers
# <div style="margin:0;padding:0;display:inline">
# <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" />
# </div>
# <input id="person_first_name" name="person[first_name]" size="30" type="text" />
# <input id="person_last_name" name="person[last_name]" size="30" type="text" />
# <label for="person_first_name">First name</label>:
# <input id="person_first_name" name="person[first_name]" size="30" type="text" /><br />
#
# <label for="person_last_name">Last name</label>:
# <input id="person_last_name" name="person[last_name]" size="30" type="text" /><br />
#
# <input id="person_submit" name="commit" type="submit" value="Create Person" />
# </form>
#
# Because of the names of the input fields, the controller gets a <tt>:person</tt>
# nested hash in +params+ with the corresponding first and last names. That hash
# is ready to be passed to <tt>Person.create</tt> like this:
# As you see, the HTML reflects knowledge about the resource in several spots,
# like the path the form should be submitted to, or the names of the input fields.
#
# In particular, thanks to the conventions followed in the generated field names, the
# controller gets a nested hash <tt>params[:person]</tt> with the person attributes
# set in the form. That hash is ready to be passed to <tt>Person.create</tt>:
#
# if @person = Person.create(params[:person])
# # success
# else
# # error handling
# end
#
# Interestingly, the exact same view code in the previous example can be used to edit
# a person. If <tt>@person</tt> is an existing record with name "John Smith" and ID 256,
# the code above as is would yield instead:
#
# <form action="/people/256" class="edit_person" id="edit_person_256" method="post">
# <div style="margin:0;padding:0;display:inline">
# <input name="_method" type="hidden" value="put" />
# <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" />
# </div>
# <label for="person_first_name">First name</label>:
# <input id="person_first_name" name="person[first_name]" size="30" type="text" value="John" /><br />
#
# <label for="person_last_name">Last name</label>:
# <input id="person_last_name" name="person[last_name]" size="30" type="text" value="Smith" /><br />
#
# <input id="person_submit" name="commit" type="submit" value="Update Person" />
# </form>
#
# Note that the endpoint, default values, and submit button label are tailored for <tt>@person</tt>.
# That works that way because the involved helpers know whether the resource is a new record or not,
# and generate HTML accordingly.
#
# The controller would receive the form data again in <tt>params[:person]</tt>, ready to be
# passed to <tt>Person#update_attributes</tt>:
#
# if
person = Person.create
(params[:person])
# if
@person.update_attributes
(params[:person])
# # success
# else
# # error handling
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录