的authenticate_user!在UsersController中不起作用<设计:: RegistrationsController

时间:2013-12-07 10:36:45

标签: ruby-on-rails devise

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     class User&lt;的ActiveRecord ::基

  # 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'

1 个答案:

答案 0 :(得分:2)

您从Devise :: RegistrationsController继承了UsersController。

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

您需要传递'force'属性才能执行。请尝试以下方法:

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