首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >authenticate_user!在UsersController中不能工作::RegistrationsController

authenticate_user!在UsersController中不能工作::RegistrationsController
EN

Stack Overflow用户
提问于 2013-12-07 10:36:45
回答 1查看 1.4K关注 0票数 2
代码语言:javascript
复制
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在哪里

代码语言:javascript
复制
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一样,这样就得到了@info的经典nilClass错误。

怎么会这样呢?

编辑

文件user.rb类用户< ActiveRecord::Base

代码语言:javascript
复制
  # Include default devise modules. Others available are:
  # :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable,
     :validatable, :confirmable, :lockable

end

文件routes.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2015-05-08 11:08:37

您从Devise::RegistrationsController继承了UsersController。

代码语言:javascript
复制
'before_action :authenticate_user!' #does not execute for devise controllers. 

要执行它,需要传递'force‘属性。尝试以下几点:

代码语言:javascript
复制
before_filter ->{ authenticate_user!( force: true ) } 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20440324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档