zer0uno
zer0uno

Reputation: 8030

authenticate_user! doesn't work in UsersController < Devise::RegistrationsController

class UsersController < Devise::RegistrationsController
  before_action :authenticate_user! #not working
  # before_action :only_signed_in_user works

  def show
  end

end

Where only_signed_in_user is

module HeartFillerHelper

  def only_signed_in_user
    unless current_user
      flash[:notice] = 'Devi essere loggato per avere accesso a tale funzione'
      redirect_to root_path
    end
  end

end

The problem is that I am getting no error when authenticate_user! is executed, I mean the action show is processed as if there was no before_action and in this way a get the classic nilClass error for @info

How is that?

Edit

File user.rb class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable,
     :validatable, :confirmable, :lockable

end

File routes.rb

HeartFiller::Application.routes.draw do
  root :to => "heart_filler#index"
  get "static_pages/about", as: 'about'
  get "static_pages/help", as: 'help'

  devise_for :users, :controllers => { :registrations => "users" }
  devise_scope :user do
    get "users/profile", :to => "users#show", :as => 'profile'
    get "users/add_credit", :to => "users#add_credit", :as => 'add_credit'
    post "users/update_credit", :to => "users#update_credit", :as => 'update_credit'
  end

  get 'campaigns/my_index', to: "campaigns#my_index", as: 'my_index'
  resources :campaigns

  get 'offers/:id/new', to: 'offers#new', as: :new_offer
  post 'offers/:id', to: 'offers#create', as: :offers
  resources :offers, only: [:show, :index]

end

Gemfile

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.1'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use Devise for authentication
gem 'devise'

# Use PaperClip for images
gem 'paperclip'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Twitter-Bootstrap for stylesheets
gem 'bootstrap-sass', '2.3.2.0'

# Use Will-Paginate for the presentation
gem 'will_paginate'
gem 'bootstrap-will_paginate'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# Use TheRubyRacer for javascript runtime
gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use Hirb for advanced console
gem 'hirb'

Upvotes: 2

Views: 1532

Answers (1)

Shriram Balakrishnan
Shriram Balakrishnan

Reputation: 509

You have inherited UsersController from Devise::RegistrationsController.

'before_action :authenticate_user!' #does not execute for devise controllers. 

You need to pass 'force' attribute for it to execute. Try the following:

before_filter ->{ authenticate_user!( force: true ) } 

Upvotes: 2

Related Questions