定制设计控制器 [英] Custom Devise controller

查看:99
本文介绍了定制设计控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为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


$ b $嗯,这很好。但是让我们说,我想完全控制在我的 #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必需字段为login,password和password_confirmation

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 file

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屋!

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