定制控制器

发布于 2024-10-11 12:44:19 字数 348 浏览 3 评论 0原文

我想为 Rails 中的 Devise 自定义我的注册控制器。我知道您必须创建一个像这样的控制器:

class AccountsController < Devise::SessionsController
  def create
    super
  end
end

嗯,这一切都很好。但是,假设我想完全控制 #create 操作中发生的情况。我该怎么做?如何手动创建模型并传递所有参数? Account.create(params[:account]) 能顺利处理吗?是否有一些我应该了解的内部内容,或者是我在操作中调用 #super 的唯一选择?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情出卖 2024-10-18 12:44:19

只要您满足您的必填字段,您就可以在示例中调用 Account.create ,我很确定默认的 Devise 必填字段是登录名、密码和密码确认

我们在 CRUD 屏幕中执行此操作以创建设备用户,

@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 文件中调整的方法

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

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

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

相对绾红妆 2024-10-18 12:44:19

如果您想确保正确地覆盖了某些内容,并确保没有遗漏任何处理,您还可以查看 Github 上的源代码...

https://github.com/plataformatec/devise/tree/master/app/controllers

You can also have a look at the source on Github, if you want to be sure you're overriding things properly, and be sure you're not missing any processing...

https://github.com/plataformatec/devise/tree/master/app/controllers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文