Rails 4 - devise_for not using custom controllers

2019-08-09 18:57发布

问题:

I use Devise in my Rails app, and I would like to customise some of it's behaviour. I have read the documentation for devise_for. But I can't get it to use my custom controllers. My routs looks like this:

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

In my Users::RegistrationsController < Devise::RegistrationsController I have tried the following:

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

Any ideas on what I'm missing?

Update

Here is my file structure:

Update 2

Think I might have found the root of the issue. I render and present my signup form in a Bootstrap modal on another route than the default Devise path.

I present the form on this path:

/apps/1/edit

And not:

users/sign_up

If I do sign up on this path users/sign_up it works and my methods get called. In my /apps/1/edit view I present the forms like this:

= render "users/registrations/modal_form"

My edit action for my apps_controller (the action where I present the signup form) is currently empty:

# GET /apps/1/edit
def edit
end

The relevant parts of my users/registrations/modal_form partial looks like this:

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

And my users/registrations/new_form looks like this:

= 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"

I'm not that familiar with Devise. Any ideas on what and where I'm missing something?

回答1:

Think I solved it. The issue was in my form_for where I used the wrong route. Instead of using the default registration_path as the url I instead use:

user_registration_path(resource_name)

Silly mistake. Ran into another issue, I now keep getting: ActionController::UnknownFormat on signup. But it is at least using my custom controllers now.