Rails 3:操纵设备“资源”在控制器中?
我正在使用 devise & devise_invitable 在 Rails 3 项目中,我正在尝试操作 devise 控制器中的一些“用户”对象字段。
操作的问题是这样的:
def update
self.resource = resource_class.accept_invitation!(params[resource_name])
resource.first_name = 'Lemmechangethis'
if resource.errors.empty?
set_flash_message :notice, :updated
sign_in(resource_name, resource)
respond_with resource, :location => after_accept_path_for(resource)
else
respond_with_navigational(resource){ render_with_scope :edit }
end
end
我本以为(注释掉的)resource.first_name 调用会以与模型大致相同的方式影响资源 - 但似乎并非如此。我在此表单上仍然收到“空白”验证错误。
所以,问题是,如何为 devise(和/或 devise_invitable)中的 User 模型指定实际需要验证的值?
任何建议表示赞赏, 约翰
I'm using devise & devise_invitable in a rails 3 project, and I'm trying to manipulate some of the 'User' object fields in the devise controller.
The action is question is this:
def update
self.resource = resource_class.accept_invitation!(params[resource_name])
resource.first_name = 'Lemmechangethis'
if resource.errors.empty?
set_flash_message :notice, :updated
sign_in(resource_name, resource)
respond_with resource, :location => after_accept_path_for(resource)
else
respond_with_navigational(resource){ render_with_scope :edit }
end
end
I'd have thought that the (commented out) resource.first_name call would influence resource in much the same way as a model - but it doesn't seem to. I'm still getting a 'blank' validation error on this form.
So, the question is, how do I specify values to the User model in devise (and/or devise_invitable) that will actually be subject to verification?
Any suggestions appreciated,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
resource
确实返回一个用户模型实例。因此,resource.first_name = 'Lemmechangethis'
语句确实更改了您的用户模型实例,但它不会触发您的用户模型验证,这可能就是为什么resource.errors
总是返回空的原因大批。例如,触发用户模型验证的一种方法是调用resource.valid?
。然后,您可以检查resource.errors
数组以获取特定的错误消息。resource
does return a User models instance. So theresource.first_name = 'Lemmechangethis'
statement does change your User models instance but it doesnot trigger your User models validations, which is probably whyresource.errors
always returns an empty array. One way to trigger the User models validation is to callresource.valid?
for example. You can then check theresource.errors
array for specific error messages.