自定义设计控制器 [英] Custom Devise controller
问题描述
我想为 Rails 中的 Devise 自定义我的注册控制器.我知道您必须像这样创建一个控制器:
I would like to customize my registrations controller for Devise in Rails. I understand that you must create a controller like this:
class AccountsController < Devise::SessionsController
def create
super
end
end
嗯,这一切都很好.但是,假设我想完全控制在我的 #create
操作中发生的事情.我怎么做?如何手动创建模型并将所有参数传递给它?Account.create(params[:account]) 会顺利处理吗?是否有一些我应该知道的内部内容,或者是我在操作中调用 #super
的唯一选择?
Well, that's all very good. But then let's say I want to fully control what happens in my #create
action. How do I do that? How do I manually create a model and pass it all the params? Would Account.create(params[:account]) handle it smoothly? Is there some internal stuff going on I should know about or is my only option to call #super
inside the action?
推荐答案
只要您填写必填字段,您就可以在示例中调用 Account.create,我很确定默认的 Devise 必填字段是登录名、密码和密码_确认
As long as you fulfil your required fields you can call Account.create in your example, I'm pretty sure the default Devise required fields are login, password and password_confirmation
我们在创建设备用户的 CRUD 屏幕中执行此操作,
We do this in a CRUD screen for creating devise users,
@admin = Admin.new(params[:admin])
if @admin.save
redirect_to admin_admins_path, :notice => 'New Administrator has been added'
else
render :action => "new"
end
并且您不想扩展 Devise 会话控制器,扩展 ApplicationController 的普通控制器很好,或者您可以扩展 Devise::RegistrationsController 并覆盖您想要在 registrations_controller.rb
文件
and you don't want to extend the Devise session controller, a normal controller extending ApplicationController is fine or you can extend Devise::RegistrationsController and overwrite the methods you want to tweak in a registrations_controller.rb
file
这篇关于自定义设计控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!