authenticate_user!在用户控制器中不起作用 < 设计::注册控制器


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

only_signed_in_user在哪里

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

问题是我在执行authenticate_user!时没有收到任何错误,我的意思是show的操作被处理得好像没有before_action,这样就会得到经典的nilClass错误@info

这是怎么回事?

编辑

文件user.rb 类 用户 <活动记录::基本>

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

文件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'

您已经从 Devise::RegistrationsController 继承了 UsersController。

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

您需要传递"force"属性才能执行。请尝试以下操作:

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

最新更新