82

I'm using devise for user auth, but I have nice mockups for the signup, login, etc. pages. I've already done the rails generate devise:views User command and have all of the views in the views folder, however, when I replaced the registration/new.html.erb with my own new.html.erb, nothing changes nor looks different. It's as if I had done anything.

Anyone know what I'm doing wrong or at least how to successfully customize devise views

P.S. Is it important to note that I changed the route of devise/registration#new to /signup?

Antony Mithun
  • 161
  • 1
  • 15
Vasseurth
  • 6,354
  • 12
  • 53
  • 81

8 Answers8

126

at a glance answer.

...instead of

rails generate devise:views User

use:

rails generate devise:views

If you've already done it, move the folders devise created from app/views/User to a new folder app/views/devise (or just rename the User folder to devise, if that's an option.)

Those folders are:

app/views/User/confirmations
app/views/User/mailer
app/views/User/passwords
app/views/User/registrations
app/views/User/sessions
app/views/User/shared
app/views/User/unlocks

No other changes are necessary.

ocodo
  • 29,401
  • 18
  • 105
  • 117
  • 4
    *This* is the answer. You cannot edit devise views by default since they are inside them gem until you generate it like that. Thank you. – Lukas Mar 22 '15 at 09:40
  • I'll buy you a cup of coffee if I ever cross your path IRL. – Thomas Ayoub May 20 '15 at 08:43
  • 5
    If you want to customize views from a specific controller, add the `-v` option like this: `rails generate devise:views -v registrations confirmations` – Chambeur Nov 04 '15 at 03:28
  • 8
    This is the answer if you have **only one model for authentication**. If you have 2 models for authentication, say **`users`** and **`admins`**, you will have to go with the answer below by @QBDSolutions – Aswin Ramakrishnan Nov 04 '15 at 17:12
118

though this is an old question, I thought I'd add to it in case anybody stumbles on it. I'm not sure if this is a new addition since the question was originally asked but if so the simpler (more modern) approach is this.

in the file config/initializers/devise.rb there is the following block of code:

# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
# config.scoped_views = false

by uncommenting config.scoped_views = false and changing it's value to true, devise will automatically check whether the custom view exists and if so, serve that up.

As the comment says, it may add some overhead to the application but in my experience so far, this is negligible.

DazBaldwin
  • 4,125
  • 3
  • 39
  • 43
  • 3
    Thank you! I've been trying to figure how to activate my "custom" views until I came across your answer. – Xander Mar 28 '13 at 05:21
  • 4
    This should be marked as the solution, as it correctly solves the original problem. – michaeldwp Oct 19 '13 at 04:16
  • I think that this could have been a new addition to devise, the answer was selected back in 2011, I just stumbled across the question whilst learning to use devise and figured that it's still a common problem that needs the new solution adding – DazBaldwin Oct 23 '13 at 11:41
  • 4
    Actually, setting this option to true does **not** add overhead **if** you are actually defining the custom views. I think you may have misinterpreted the meaning of the comment in the file. What it is actually pointing out is, by default, there is no reason to have custom views turned on. Therefore, if you turn them on and don't actually define the new views, it will look it see if they exist, **then** it will fallback to the default, which would then cause the overhead. So, as long as you define the custom views, setting this option to true theoretically should not add any overhead. – mkralla11 Feb 10 '15 at 17:41
  • 1
    This is the correct answer ruby concept is to go DRY. – vidur punj May 07 '17 at 17:10
  • 1
    Great. answer! I had a model called 'Events' and was stumped for a long time why Rails wouldn't route to my events controller but instead to devise/events. seems like the option you described, make Rails check if i have an events path first before going to devise/events – ChairmanMeow Jan 01 '19 at 21:31
37

Your route signup or devise/registrations#new will render the view views/devise/registrations/new.html.erb. It sounds like you made changes to views/user/registrations/new.html.erb, which would explain why you dont see the changes made since its not being rendered.

You will either need to create a user/registrations_controller.rb that extends from Devise::RegistrationsController and point your /signup route to user/registrations#new, or you can just make your changes directly to views/devise/registrations/new.html.erb

Same idea applies to your login (devise/sessions) pages.

Hope this helps.

ocodo
  • 29,401
  • 18
  • 105
  • 117
Kevin Tsoi
  • 1,807
  • 15
  • 16
  • How will i differentiate from users and admin when I do that if there isn't /users/registration or /admin/registrations? – Vasseurth Jul 11 '11 at 08:03
  • 3
    rails generate devise:views User already generated the /users/registrations for you already correct? If you have an admin role as well, then you have need to generate the admin views also (rails generate devise:views admin). – Kevin Tsoi Jul 11 '11 at 08:21
25

For anyone still having a problem with this, the problem lies in the call to rails generate devise:views User. It should be rails generate devise:views for fetching current views from the Devise Rails Engine. This will generate proper views which will work with the default routes.

ocodo
  • 29,401
  • 18
  • 105
  • 117
Marcus W
  • 589
  • 6
  • 11
  • How can I go back? I runned `rails generate devise:views User` it generated a folder `Users` in the view and also a controller folder `users`. When I changed the name to `devise` it doesn't work? – Steven Aguilar Sep 16 '18 at 20:44
22

After generating your custom views e.g

rails generate devise:views User

Turn on scoped_views in config/initializer/devise.rb

view config.scoped_views = true

And you are done.

ocodo
  • 29,401
  • 18
  • 105
  • 117
Saqib R.
  • 2,919
  • 2
  • 26
  • 21
14

Using rails g devise:views User allows you to customize when you have more than one role.

the proper way to do this is going into your devise.rb in config/initializer/ folder

and uncommenting and setting config.scoped_views = true.

now you can edit the view erb files without any problems

ocodo
  • 29,401
  • 18
  • 105
  • 117
Richard Lau
  • 696
  • 9
  • 22
14

I had the same problem until I went back and read the devise documentation :)

After rails generate devise:views make sure you go into initializers/devise.rb and set config.scoped_views = true. This is explained in the devise documentation at https://github.com/plataformatec/devise as well as in the devise.rb comments.

After I did this, my own views in views/users started showing up instead of the ones in the gem.

Serhii Nadolynskyi
  • 5,473
  • 3
  • 21
  • 20
guero64
  • 1,019
  • 1
  • 12
  • 18
2

For future reference, you can just rename folder from devise => user and vice versa and rails will find a route.

  • 1
    I believe that this is equivalent to doing `rails g devise:views User` except that it adds the new folder alongside the devise one. as for allowing rails to find a route, I don't think that is a very good habit to fall in to – DazBaldwin Oct 23 '13 at 11:44