问题描述
我是一个相对新手,所以非常感谢任何帮助.
我正在使用 Rails 4.2,并使用 Clearance gem 进行身份验证.我希望有人可以描述覆盖控制器以在 sign_up 表单上包含自定义属性的最佳实践.
我已经阅读了很多带有各种建议的建议,其中许多来自不使用 strong_parameters 的早期版本的 rails.
如果有人能提供我需要克服的方法的简要分类(user_params/user_from_params/etc),我将不胜感激.我可以通过定义一个仅包含@user = User.new 的新新"方法和使用 .permit 的新user_params"方法来使事情正常运行,但我担心我在 user_from_params 中绕过的默认代码.
关于最佳实践的任何建议都会很棒!
谢谢
当我有时间更新 Clearance 的文档时,我正在工作,但现在最简单的事情是检查 清除用户控制器以查看可以覆盖的各种方法.>
user_from_params
的默认实现现在有点奇怪,因为 Clearance 1.x 仍然支持 Rails 3.x,所以它是在我们使用 attr_accessible
的时候写的而不是强参数.
我可能会这样做:
def user_params# 使用强参数来做你需要的结尾def user_from_params用户.new(user_params)结尾
您可能需要稍微调整一下 user_params
,这样无论您做什么,对 new
操作仍然有效,因为 user_from_params
仍然是在那里使用.我打开了一个清除问题,看看我们是否可以在 2.0 之前改进这方面.
I am a relative newbie so would really appreciate any assistance.
I'm using Rails 4.2, with the Clearance gem for authentication. I'm hoping someone could describe the best practise for over-riding the controllers to include custom attributes on the sign_up form.
I've read a lot of suggestions with varied advice, many of which are from previous versions of rails that do not use strong_parameters.
If anyone could provide a brief breakdown of the methods I need to be over-riding (user_params/user_from_params/etc) I would be most grateful. I can get things functioning by defining a new 'new' method that just includes @user = User.new, and the a new 'user_params' method using .permit, but I'm concerned about the default code I'm bypassing in user_from_params.
Any advice on best practise here would be fantastic!
Thanks
I'm working when I have time on updating the docs for Clearance, but right now the simplest thing to do is to inspect the Clearance users controller to see the various methods that can be overridden.
The default implementation of user_from_params
is a bit odd now because Clearance 1.x still supports Rails 3.x, so it was written for a time when we used attr_accessible
rather than strong parameters.
I'd probably do something like:
def user_params
# Use strong params to do what you need
end
def user_from_params
User.new(user_params)
end
You may need to massage user_params
a bit so that whatever you do there is still valid for the new
action, as user_from_params
is still used there. I've opened an issue in Clearance to see if we can improve that aspect before 2.0.
这篇关于rails 4 和thoughtbot/clearance - 向用户模型添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!