vkill'blog

“技术本身没有太多价值,掌握了新的思考方式才是真的收获”

rails scaffold 生成view的new和edit 中的 form_for 为啥一样?

18:31 , vkill
rails 中当我们用 scaffold 后,生成view的 new和edit 中的 form_for 为啥一样?
一样了怎么知道哪个是update?哪个是create呢?

我想是根据@project来判断的
因为在controller中@project的定义不同
Quotation

def edit
  @project = Project.find(params[:id])
end
def new
  @project = Project.new
end

edit的时候 @project.id 为 true ,相应的action也就不一样,还会多生成一个input来标识put,因为此时是 update

<form action="/projects/4" class="edit_project" id="edit_project_4" method="post">
<input name="_method" type="hidden" value="put" />

new的时候 @project.id 为 false

<form action="/projects" class="new_project" id="new_project" method="post">

最后,可以共用form是因为生成input后都会去填充,@project.xxx 为nil时也是填充了,只是啥都没有
Last modified by vkill on2010/07/13 18:33
类别:ruby & rails | Tags: , | 0 条评论, 1146 次阅读
网友评论(0):
发表评论:

Nickname: 
Email:
Site URI: