Devise and ActionMailer confusion - ruby-on-rails

I'm using Devise for authentication, and I'm confused on how to set up mail along with it. Should you still be creating your own mailer and initializer file, or should you be sending all mail through Devise? Where do you go in Devise to create the email template and the method for sending the email?
I realize this is kind of a broad question, so essentially I'm asking what is the best way to set up mail with Devise?
Also, if you wanted to send an email to a user after they have confirmed their email, how would you do this?

Devise does create it's own Mailer--if you take a look on GitHub https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb you can see that it comes with a bunch of methods already packaged in.
You can go ahead and generate the views for these methods using the command
rails g devise views
and then edit them.
If you would like to send additional email messages you should create your own Mailer to do so. I would recommend http://edgeguides.rubyonrails.org/action_mailer_basics.html . It's a pretty good overview of how to set up a mailer from the ground up.

Devise creates the mailer and email templates for you, so you don't have to worry about that. However, if you want to change the email templates, install devise views using the command:
rails g devise:views
This will add a new "devise" folder in your views. You can find all the email templates in the mailer folder under views/devise.
Use the confirmable attribute to send confirmation emails to users post registration.
By default, this attribute is commented out. So, once you install devise using the command rails g devise:install, go to db/migrate and locate the devise_create_users migration and uncomment the following lines:
t.confirmable and
add_index :users, :confirmation_token, :unique => true.
Once done, migrate your database.
Now go to your user model and check if devise has the :confirmable attribute, if not add it and you are all set.

Related

Adding api to an existing rails project with devise

I need to add some api for moobile to my existing project in rails. I am using devise gem for authentication. The first api needed are user registration, login, profile update , some posting feature etc.I am following https://github.com/lynndylanhurley/devise_token_auth this to create api, but it creates user.rb and migration as well as a duplicate routes. Am I doing something wrong. Please help me to solve the issue . Thanks in advance
I have added devise token authentication for api. Also created a seperate application controller for api's. All the api controller extends this application controller. The api routes starts with /api/
Documentation says:
A model will be created in the app/models directory. If the model already exists, a concern will be included at the top of the file.
And
A migration file will be created in the db/migrate directory. Inspect the migrations file, add additional columns if necessary, and then run the migration:
So
Can I use this gem alongside standard Devise?
Yes! But you will need to enable the support of separate routes for standard Devise.
https://github.com/lynndylanhurley/devise_token_auth#can-i-use-this-gem-alongside-standard-devise
Personally, I wouldn't use Devise for your authentication but would create a custom one next to Devise just for your API. Devise can become a bit buggy later on in the process when using it for API-authentication. Then for your authorization you could use Pundit. You might want to use Regulator next to it for controller namespaced authorization polices(it's not under development anymore, but it does the job).
There's a nice tutorial about this process:
API Tutorial
Here you can find Pundit:
Pundit Gem
And here's the Regulator gem:
Regulator Gem

Where is the code that Devise generates?

I installed the Devise gem into my Rails app, and ran rails generate devise:install and rails generate devise User.
Without my doing anything, the url users/sign_up already has a view somehow. The problem is, I can't find the template that is being rendered anywhere. It's certainly not under app/views/users. I chose some text on the page and ran a search for it within my app, and got back 0 results.
Then I tried to sign up with the form, and got the following error:
NoMethodError in Devise::RegistrationsController#create
undefined method `current_sign_in_ip' for #<User:xxxxxxxxxxx>
I then searched for this controller, but there is no RegistrationsController in my app, and no Devise file. None of the files I'm looking for were generated by the two commands I mentioned above, either.
The Devise documentation doesn't seem to shed any light on where the Devise code is kept.
Is the code even in my app? I'm so confused.
Using Devise, you're able to generate the templates which Devise depends on for logins, password resets, etc. using the following command:
rails generate devise:views
This will create copies of the templates for Devise in your views directory.
For controllers, you can access/override their functionality by subclassing them in your own code. They're under the Devise namespace:
class NewRegistrationsController < Devise::RegistrationsController
# do stuff here
end
Then point the router to use this new controller:
devise_for :users, controllers: { registrations: 'new_registrations' }
The code for the controllers can be found in Devise's source code - you can reference it to better understand what each controller is doing.
Hope that helps!
This is standard practice for Rails "engines" (which almost all gems are) -
Think of them as libraries / dependencies... wherein they provide access to a lot of pre-compiled functionality through several hooks (often provided by an API).
One of the reasons I'd actually recommend people to write their own gem is because it helps you appreciate how the whole thing works. I wrote a gem, it uses views just like Devise:
These views are not seen in the application because they're appended to your Rails app at runtime. It's basically how the PATH var works in cmd, if you've ever had the pleasure of working with programmatic compilation etc.
Thus, Devise's "views" are stored in the Devise gem. This is appended to your Ruby installation... [Ruby install dir]/lib/ruby/gems/[ver]/gems, loaded at RunTime just like the PATH var...
Whilst you can generate your Devise views (as mentioned in the other answers), this is the base line of how it's able to access them without any prior references.
NoMethodError in Devise::RegistrationsController#create
undefined method `current_sign_in_ip' for #<User:xxxxxxxxxxx>
This means you don't have the current_sign_in_ip attribute for your Devise installation. I answered your question about this specifically here...
Devise error: undefined method `current_sign_in_ip'
All the devise MVC files are inside the gem. Below is my devise views directory. You could check yours as well. Go to your project root.
gem show 'devise'
/Users/saurabh/.rvm/gems/ruby-2.1.0/gems/devise-3.2.4
cd /Users/saurabh/.rvm/gems/ruby-2.1.0/gems/devise-3.2.4/app/views
You can generate views in your project if you wish to customize.
rails generate devise:views
All code of devise can be easily go through by devise and if you are using rubyMine you can view devise code in external libraries in devise folder.
To generate template for your model
rails generate devise:views
and then you can change your view as you want for your application.

Once I have devise installed, how do I add (and remove) modules easily? - Rails 3

I just added :confirmable but am now getting an error:
NameError in Devise::RegistrationsController#create
undefined local variable or method `confirmed_at' for #<User:0x000001025db288>
I realized that after I added it, I didn't do anything that would generate a migration.
How do I get a new migration to alter the current schema on an existing devise install?
Thanks.
Update: Here's Devise wiki page on how to change existing table to suit Devise needs:
For Devise below 2.0
https://github.com/plataformatec/devise/wiki/How-To:-change-an-already-existing-table-to-add-devise-required-columns
For Devise after 2.0
https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style
I would suggest you to take a look on this topic.
It might help you.
So it seems the best way to do this is via this Devise Wiki link.
I just tested it and it seems to work.

Customizing Devise views in Rails

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?
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.
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.
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.
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.
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.
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.
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
For future reference, you can just rename folder from devise => user and vice versa and rails will find a route.

how to change devise's flash[:notice] in rails

I'm using rails 2.3.5 and devise 1.0.6.
I'm having users confirm account's with email.
However, when a new user sign's up the flash notice says "The user was successfully created" which it was but it doesn't tell the user that they need to confirm their their email unless they try to log in and devise's flash notice still doesn't explain that they have to confirm through email.
Where is this flash notice located in the gem and how can I change it? Or what is a better way to fix this problem.
Just enable i18n in your project and edit the default locale (en.devise.yml) or download new ones from
https://github.com/plataformatec/devise/wiki/i18n
Flash messages for the devise gem can be altered in the locales directory (YourRailsApp/config/locales/devise.en.yml).
Just change the devise.en.yml to your liking, it doesn't really take any programming knowledge.
If you are new to rails the locales directory is for I18n translations which is rails way of translating words albeit statically but it works well for simple projects.

Resources