Luis Crespo
Luis Crespo

Reputation: 1620

Rails Generate Devise:Controllers not Working

Simple question.

I'm using Rails 4.1.4 and Devise 3.3.0 for my app.

I'm trying to generate Devise's controllers so I can override some behaviour.

Documentation says to run...

rails generate devise:controllers [scope]

... to generate controllers under app/controllers/scope so you can then modify them. But when I run the previous command it keeps saying that there is no generator devise:controllers:

Could not find generator devise:controllers.

Does anyone knows why?.

Thanks.

UPDATE

In fact, when I run...

rails generate

... to retrieve a list of the available generators, I get the following output for Devise generators:

Devise:

devise

devise:install

devise:views

So definitelly, the devise:controllers generator isn't there. Is there a way to add it?. How?.

Thanks.

Upvotes: 8

Views: 11186

Answers (4)

stevec
stevec

Reputation: 52548

Putting this here in case anyone else has this (silly) problem. I couldn't work out why a POST to /users kept routing to Devise::RegistrationsController#create rather than Users::RegistrationsController#create

The reason?

I had a typo in routes.rb

  devise_for :users, controllers: { registations: 'users/registrations }

Note I had registations instead of registrations

So be wary you may have a typo in your route somewhere

Upvotes: 0

sealocal
sealocal

Reputation: 12427

To answer the OP's original question of "Does anyone knows why?"

The problem is that this generator is currently only available on Devise's master branch, as stated on this GitHub issue.

If you want to use this feature before it's published, you could add this to your Gemfile:

gem 'devise', git: 'https://github.com/plataformatec/devise'

Upvotes: 3

Luis Crespo
Luis Crespo

Reputation: 1620

SOLVED

I've just created the controller manually and make it inherit from Devise. For example:

class Users::RegistrationsController < Devise::RegistrationsController
    # Override the action you want here.
end

This controller should live in app/controllers/users/registrations_controller.rb. If you have any other scope just go with app/controllers/scope/registrations_controller.rb. For example if you have an admin scope it would be app/controllers/admins/registrations_controller.rb.

Best.

UPDATE

Following the comment from blushrt, I forgot to mention that it is important to modify config/routes.rb to make Devise use the created controller for the specific resource. For example, for users, you should put in your config/routes.rb:

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

That's it. Best.

Upvotes: 15

Joel
Joel

Reputation: 4603

https://github.com/plataformatec/devise/wiki/Tool:-Generate-and-customize-controllers

You can run this command in your terminal.

bash <(curl -s https://raw.githubusercontent.com/foohey/cdc/master/cdc.sh)

Upvotes: 2

Related Questions