【问题标题】:Rails 4 - devise_for not using custom controllersRails 4 - devise_for 不使用自定义控制器
【发布时间】:2015-11-21 13:52:40
【问题描述】:

我在我的 Rails 应用程序中使用 Devise,我想自定义它的一些行为。我已经阅读了devise_for 的文档。但我无法让它使用我的自定义控制器。我的溃败看起来像这样:

devise_for :users, controllers: {
  sessions: "users/sessions",
  registrations: "users/registrations"
}

在我的Users::RegistrationsController < Devise::RegistrationsController 中,我尝试了以下方法:

# POST /resource
def create
  puts "I'm here" # never happens!
  super
end

关于我缺少什么的任何想法?

更新

这是我的文件结构:

更新 2

我想我可能已经找到了问题的根源。我在不同于默认设计路径的另一条路线上以 Bootstrap 模式呈现和展示我的注册表单。

我在这条路径上展示表单:

/apps/1/edit

而不是:

users/sign_up

如果我在这条路径上注册users/sign_up,它会起作用并且我的方法会被调用。在我的/apps/1/edit 视图中,我呈现如下形式:

= render "users/registrations/modal_form"

我对@9​​87654333@ 的编辑操作(我提交注册表单的操作)当前为空:

# GET /apps/1/edit
def edit
end

我的users/registrations/modal_form 部分的相关部分如下所示:

.modal.fade#registrations_modal{ tabindex: -1, role: 'dialog', "aria-labelledby" 
  [..]
  = render "users/registrations/new_form"
  [..]

而我的users/registrations/new_form 看起来像这样:

= form_for(resource, as: resource_name, url: registration_path(resource_name))  do |f|
  = devise_error_messages!
  .form-group
    = f.label :name
    = f.text_field :name, autofocus: true, class: "form-control"
  .form-group
    = f.label :email
    = f.email_field :email, class: "form-control"

  .form-group
    = f.label :password
    - if @minimum_password_length
      %em
        (#{@minimum_password_length} characters minimum)
    = f.password_field :password, autocomplete: "off", class: "form-control"

  .form-group
    = f.submit "Sign up", class: "btn btn-default"

- if current_page?(new_user_registration_path)
  = render "devise/shared/links"

我对设计不是很熟悉。关于我遗漏了什么以及在哪里遗漏了什么的任何想法?

【问题讨论】:

  • 尝试放置puts "I'm here"以外的其他东西,尝试放置debugger,看看您的应用是否在创建操作时暂停?还是直接把super去掉,看看create的action是否还能正常工作?
  • 尝试使用byebug gem (guides.rubyonrails.org/debugging_rails_applications.html) 设置断点。但似乎它永远不会被调用。
  • 你试过重启rails服务器吗?你把Users::RegistrationsController放在哪里?
  • 试试这个Users::RegistrationsController < Devise::RegistrationsController
  • 如果您在自定义控制器中创建诸如new 之类的GET 操作并使其输出诸如return "hello" 之类的一些文本,会发生什么?

标签: ruby-on-rails ruby devise


【解决方案1】:

我想我解决了。问题出在我的form_for 中,我使用了错误的路线。我没有使用默认的registration_path 作为url,而是使用:

user_registration_path(resource_name)

愚蠢的错误。遇到另一个问题,我现在不断收到:ActionController::UnknownFormat 注册。但它至少现在正在使用我的自定义控制器。

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多