在 Ruby on Rails Restful Authentication 中,UsersController#new 使用了 @user = User.new.真的需要吗? [英] In Ruby on Rails Restful Authentication, UsersController#new, a @user = User.new is used. Is it actually needed?

查看:46
本文介绍了在 Ruby on Rails Restful Authentication 中,UsersController#new 使用了 @user = User.new.真的需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之后

script/generate authenticated user sessions

users_controller.rb 是用

def new
  @user = User.new
end

并且视图有这一行:

@user.password = @user.password_confirmation = nil

就是这样.这真的需要吗?我的意思是表单将通过 RESTful 路由 POST 到 /users,转到 UsersController#create,所以实际上创建的 @user 永远不会用过的.它真的需要吗?为什么?谢谢.

and that's it. Is this actually needed? I mean the form will POST to /users which is by RESTful routing, going to UsersController#create, so the @user created actually is never used. Is it actually needed and why? thanks.

更新: @user 再也不会在其他任何地方使用了......此外,我尝试删除这两行

Update: @user is never used again any where else... also, I tried removing those two lines

@user = User.new

@user.password = @user.password_confirmation = nil

而且我仍然可以使用表单来创建新用户...

and I can still use the form to create a new user...

推荐答案

从这个角度来看,它有点道理.假设用户帐户创建失败 - 您将使用不同的(不是新的)@user 对象重新呈现 new 视图.我可能会在操作中重置密码和 password_confirmation.

In the view it kind of makes sense. Let say user account creation fails - you'll be re-rendering the new view with a different (not new) @user object. I'd probably reset the password and password_confirmation in the action.

这篇关于在 Ruby on Rails Restful Authentication 中,UsersController#new 使用了 @user = User.new.真的需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆