user2382595
user2382595

Reputation: 51

how to overwrite devise controllers?

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.

Upvotes: 0

Views: 802

Answers (2)

mkk
mkk

Reputation: 7693

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?

Upvotes: 1

Substantial
Substantial

Reputation: 6682

Watch your spelling. The before_filter is calling a method that doesn't exist.

enter image description here

Upvotes: 0

Related Questions