I have the devise SessionController overwrited:
on app/controllers/customers/sessions_controller.rb
class Customers::SessionsController < Devise::SessionsController
before_filter :destroy_cart, only: :destroy
def destroy_cart
cart = Cart.find(current_client.cart.id)
cart.destroy
end
end
but the cart is never destroyed, even if I overwrite the destroy method directly and add the super after my code, the cart its still there, in the database (I knkow I could create the cart just once and get it when the user logs in again or create a new one when he use the app for first time, but I want to try it this way for now), is like if is not reading my code on that SessionController.
and for some reason even when I have my views this way:
app/views/customer/registrations
the changes that I do on that views are only reflected if I change it to
app/views/devise/registrations
my routes.rb is:
devise_for :clients, :controllers => { sessions: 'customers/sessions'}
devise_scope :client do
root to: "customers/Sessions#new"
end
the model that I am using with devise is Client
why I cant destroy the cart in the devise controller?
and why I cant use the views/customer/sessions if the documentation it says I can/have to do it?
thank you for reading.
you can always try to do
def destroy
cart = Cart.find(current_client.cart.id)
cart.destroy
super
end
but first you might want to ensure that you really overwritten devise's controller correctly.
The reason why you can't see changes done to app/views/customer/registrations is beacuse you seems to overwrite only :sessions controller, so you need to change
devise_for :clients, :controllers => { sessions: 'customers/sessions'}
to
devise_for :clients, :controllers => { registrations: 'customers/registrations', sessions: 'customers/sessions'}
The last question is:
" why I cant use the views/customer/sessions if the documentation it says I can/have to do it? "
You have a typo here, you are using customers namespace, not customer in routes.rb [ sessions: 'customers/sessions' ] - just a typo?
Watch your spelling. The before_filter is calling a method that doesn't exist.
Related
Before devise, I had a model called Participant which contained info for users. It had a controller and set of views to go with it. I added devise, and asked that it used Participant for the user records. That seemed to work just fine.
Now in my world, the route to create a participant looked like this: ./program/2/participant/new because any participant other than the singular "administrator" is created and used always within a single program. The participant model already has a belongs_to :program.
My Routes look like this:
devise_for :participants
root to: 'programs#index'
resources :programs do
resources :participants do
resources :rounds do
get 'survey' => 'rounds#present_survey'
put 'survey' => 'rounds#store_survey'
end
end
resources :questions
resources :rounds
member do
get 'report' => 'reports#report'
end
end
I am a little confused about the structure of things. When I bring all the views from devises engine into my app, I get views/devise/registrations/edit and new. I want them to be /view/participants/edit and new.
And I want the routes and all that to behave accordingly. When I create a new Participant, I will know from the route what Program it is in, and be able to set up the program_id correctly. When the user logs in, unless they are "admin" I want them to be redirected to the route like ./program/3.
I am not sure how to approach this. Can you give me some tips, that would be appreciated!!
-- Pito
you have to do something as follow
class ParticipantsController < Devise::RegistrationsController
def new
... # your code of new
end
def update
... # your code of update
end
end
and in routes
devise_for :users, :controllers => { :registrations => "participants" }
hope it would help
I was wondering what the best way to add extra functionality to Devise's session#destroy action would be.
To give some context, I'm making a website where Users have Carts, and when the user's session either times out or he logs out, I want his Cart to be labeled as inactive.
I found this but I'm a bit hesitant to override the Devise controller, as it seems a bit messy...
Are there any other ways to set this Cart to inactive when a user's session is destroyed?
I do suggest you derive your controller from devise and hook onto the action, so you can safely keep away from devise's internals.
# routes.rb
devise_for :users, :controllers => { :sessions => "sessions" } # etc
# sessions_controller.rb
class SessionsController < Devise::SessionsController
after_filter :set_cart_inactive!, :only => :destroy
def set_cart_inactive!
unless user_signed_in? # logout successful?
# code here
end
end
end
I have a pretty straight forward app with a couple tweaks to Devise.
First I created a Registrations controller that class RegistrationsController < Devise::RegistrationsController inherits from Devise. I created this controller so that I could edit users without re-supplying passwords. https://gist.github.com/1514687
I also did this in my routes:
devise_for :users, :controllers => { :registrations => "registrations" }`
The signup works fine but when I call the following:
<p class="edit"><%= link_to "Edit", edit_user_registration_path(user) %></p>
The url it spits out is (running on localhost): http://localhost:3000/users/edit.2
Any ideas here?
I recommend a non-devise controller for doing this, and name it something other that "users" for the sake of not overlapping with devise routes
some key nomenclature:
rails g controller accounts
resources :accounts
def edit
#user = User.find(params[:id]
end
(other controller actions similar, just refer to #user and don't worry about that this happens to be called the accounts controller)
In your routes.rb file make sure that you have
resources :users
after your devise_for line like below:
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users
I'm working on a rails site using devise, where we do not want user sign ups just yet. User authentication is so we can login to access restricted parts of the site and add/edit things as we see fit. So for now, I created the following controller:
class Users::RegistrationController < Devise::SessionsController
def new
end
end
And setup my routes in this fashion:
devise_for :users, :controllers => { :registration => "users/registration" }
However, when I run rake routes, I still see a returned value for the create action on the registration controller. Any ideas on how to get rid of it?
Try using :registrations instead of :registration. Also, it seems like your custom controller class should be defined via:
class Users::RegistrationsController < Devise::RegistrationsController
I've seen similar posts already, but couldn't quite get the answer I needed.
I have a User model and using STI a Student model that is a type of User.
When I create a new Student, Devise logs in that Student with a student_session. The problem is the rest of my app uses a user_session. SO, should I create a new user_session using the student_session? and then logout the student?
Or is there a way to get Devise to allow a student creation, but login as the User base model?
Thank you,
Anthony
Check out this post and see if it helps you:
Rails: Using Devise with single table inheritance
The summary is essentially to do the following:
config/routes.rb:
devise_for :users, :controllers => { :sessions => 'sessions' }, :skip => :registrations
devise_for :students, :skip => :sessions
app/controllers/sessions_controller.rb:
class SessionsController < Devise::SessionsController
def create
rtn = super
sign_in(resource.type.underscore, resource.type.constantize.send(:find, resource.id)) unless resource.type.nil?
rtn
end
end
For this you have to create a custom controller.
Just an example implementation
#POST create
def create
student = Student.create(params[:student]) # normal crud with whatever your form params
sign_in(User.find(student.id)) # this actually signs in the user
# now redirect the student to the dash board / whatever page manually
end