I'm trying to apply this post ( Devise update user without password ) for users don't need to insert password to update informations.
But, I'm very confused where is this controller. Devise don't create any controller in my app/controller folder. I search in all the folders but I cant find.
Where I that controller?
I see posts talking about create a new controller, but I just want to modify little things.
You don't edit (or shouldn't) the Devise controllers. Instead you create your own controller and inherent from the Devise controller.
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def update
# add custom update logic here
end
end
Notice how RegistrationsController inherets from Devise::RegistrationsController. Now you can overide the registration methods (or modify them and call super). Even the page you are referencing about overriding the devise default behavior relies on class inheritance.
JTG offers good advice, you shouldn't edit the gem directly. As a more direct answer to the question:
To print the folder of your gem:
$ bundle show devise
To open the gem in a text editor ( while being sure not to change anything )
$ bundle open devise
Related
I'm trying to add this callback to my User model, which I generated using Devise.
before_save :check_invite_code
def check_invite_code
if self.invite_code == 'first20'
User.save
else
{icon: 'error', message: "Sorry that's not a valid invite code"}
end
end
The issue I am having is with passing the returned hash with the else block back to my view. Typically I'd be able to use the icon and message in the flash in my controller. I'm not sure how to do that. I don't have a UsersController because devise takes care of a the routing with the path being devise/controller#action. So do I create a devise directory inside of controllers, then create the corresponding controllers like sessions, etc and override the devise methods? Looking for some guidance from someone with devise experience.
If you want to customize the UserController you can easily do that by
rails generate devise:controllers [scope]
For example
rails generate devise:controllers users
Check out the documentation here
I am trying to add a new controller a Ruby on Rails 4 Spree -Ecommerce application. First of all, in routes.rb I added root :to => 'login#login' then in app/controllers/ I added a file called login_controller.rb and in the file I added the following code.
module Spree
class LoginController < Spree::StoreController
def login
render('spree/shared/_login')
end
end
end
When I start the server and go to localhost:3000/ I get this error
Unable to autoload constant LoginController, expected superclass mismatch for class LoginController
My goal here, is to require login to even view the home page of the store. I am attempting to build a site where users get a login screen when the go to it unless they are already logged in.
Please know that I am ruby noob and this is actually my first ruby on rails application so I am completely clueless here.
Also if there is a better way to go about doing what I want (requiring a login to basically view any page on the site) than my current apporach (having a LoginController which checks if there is a user logged in - if so redirect to home, if not redirect to Login) please let me know.
P.S. I got the layout for the controller from the home_controller.rb in the spree gem
I have not tested the code, but I suggest you use Spree's Devise integration
then you can add a before filter mandating authentication. Create a decorator to contain this logic addition. Create a file called base_controller_decorator.rb inside app/controllers/spree with the following code:
Spree::BaseController.class_eval do
before_filter :check_logged_in
def check_logged_in
unless spree_current_user
redirect_to spree_login_path
end
end
end
I guess if you are in the Spree module you don't need the namespace for the superclass class LoginController < Spree::StoreController like so class LoginController < StoreController.
I am using Devise and Rails 4. I want to add multiple User Models(admin, usertype1, usertype2) such that they inherit from the main User Model. I have searched many posts and come to the conclusion that I may use CanCan, which I do not want, or I may use Single Table Inheritance.
The way I see it is to add a type string-column to my main User model I created with Devise. I will also need to extend each sub-class from the parent as in:
class Admin < User
end
class Usertype1 < User
end
class Usertype2 < User
end
My question is: what do I do next? How exactly do I know how to access the type column? Do I also need to override the Devise Controller for the current_user helper method such that I can have current_admin for example?
I'm not sure this really answers the original question. I am trying to set up multiple session controllers for Devise, and it seems like it really does require multiple models for this use case. Will post back when I've got a working version.
You can also use the easy_roles gem. It is available on github. This gem provides a bitmask solution for your different user roles. You just must have one model, e.g User, this model gets an attribute "role". Just checkout the docs on github.
Devise + easy_roles + CanCan is a very good setup, it is very convenient in my opinion. I use this quite often.
Link to github: https://github.com/platform45/easy_roles
STI will give you current_admin, current_user1 and current_user2 Devise methods.
In application_controller.rb, create a custom current_user method like this:
def current_user
if current_admin
current_admin
elsif current_user1
current_user1
else
current_user2
end
end
helper_method :current_user
You will need some work on routes.rb and a custom sessions_controller.rb. See the accepted answer here Rails: Using Devise with single table inheritance
I'm using devise and acts_as_tenant gem and it seems the devise controllers don't inherit from ApplicationController (???) so don't have access to set_current_tenant_by_subdomain
When the password reset link is clicked I get no Tenant scoping by subdomain.
So how do you handle this? Is there an easy way to open up the DeviseControllers to ensure
set_current_tenant_by_subdomain is called?
Thanks so much
There are a few steps to solve this.
Customize the Devise views to include the tenant details. You can do it by using rails generate devise:views and editing the generated views.
Patch DeviseController so that it includes set_current_tenant_by_subdomain. DeviseController is inherited by the device controllers (confirmations, registrations, forgot password, etc.)
The patching would look something like this:
class DeviseController < Devise.parent_controller.constantize
set_current_tenant_by_subdomain(:account, :subdomain)
end
After installing Devise, there are routes to
/users/sign_in
/users/sign_up
/users/sign_out
but there is no file app/controllers/users_controller.rb? why does this require no controller file or is it just some where else?
The controller file is located within the gem, and you don't need to write one yourself. Most everything can be done via configuration. See https://github.com/plataformatec/devise#readme for more specific details.
You can, however, generate the views so that you may override them yourself:
rails generate devise:views
That will place files in app/views/devise that you can modify for all of the forms, etc. that Devise provides.
It's bundled with the gem. You can generate a controller 'User' separately with further actions:
class UserController < ApplicationController
def show
#user = current_user
end
end