Customizing Devise views in Rails

80,670

Solution 1

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.

Solution 2

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.

Solution 3

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 it says it does add some overhead to the application but in my experience so far this is minimal.

Solution 4

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.

Solution 5

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.

Share:
80,670

Related videos on Youtube

Vasseurth
Author by

Vasseurth

SOreadytohelp

Updated on July 05, 2022

Comments

  • Vasseurth
    Vasseurth almost 2 years

    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?

    • Gal
      Gal almost 13 years
      Did you try emptying your cache? reloading? restarting server?
    • Vasiliy Ermolovich
      Vasiliy Ermolovich almost 13 years
    • Vasseurth
      Vasseurth almost 13 years
      I figured it out... I can't have Users at the end
    • Colin
      Colin over 5 years
      @Vasseurth: to clarify, you mean don't put "User" at the end of the rails generate command because it generates a scoped view, which is not what you wanted.
  • Vasseurth
    Vasseurth almost 13 years
    How will i differentiate from users and admin when I do that if there isn't /users/registration or /admin/registrations?
  • Kevin Tsoi
    Kevin Tsoi almost 13 years
    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).
  • Xander
    Xander over 11 years
    Thank you! I've been trying to figure how to activate my "custom" views until I came across your answer.
  • michaeldwp
    michaeldwp over 10 years
    This should be marked as the solution, as it correctly solves the original problem.
  • DazBaldwin
    DazBaldwin over 10 years
    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
    DazBaldwin over 10 years
    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
  • mkralla11
    mkralla11 over 9 years
    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.
  • Lukas
    Lukas over 9 years
    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.
  • Thomas Ayoub
    Thomas Ayoub about 9 years
    I'll buy you a cup of coffee if I ever cross your path IRL.
  • Chambeur
    Chambeur over 8 years
    If you want to customize views from a specific controller, add the -v option like this: rails generate devise:views -v registrations confirmations
  • Aswin Ramakrishnan
    Aswin Ramakrishnan over 8 years
    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
  • Ayman Salah
    Ayman Salah over 8 years
    This should be the answer
  • vidur punj
    vidur punj about 7 years
    This is the correct answer ruby concept is to go DRY.
  • Steven Aguilar
    Steven Aguilar almost 6 years
    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?
  • ChairmanMeow
    ChairmanMeow over 5 years
    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