ruby-on-rails - 验证用户!在 UsersController < Devise::RegistrationsController 中不起作用

标签 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 code> 并以这种方式获得 @info

的经典 nilClass 错误

怎么样?

编辑

文件user.rb 类 User

  # 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

gem 文件

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 ) } 

关于ruby-on-rails - 验证用户!在 UsersController < Devise::RegistrationsController 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440324/

相关文章:

ruby-on-rails - Rails paper_trail 与 Active Storage 导致重复版本

ruby-on-rails - 将模型属性限制为 Rails 中的值列表的方法是什么?

ruby-on-rails - rails 阻止在 before_create 回调中创建对象

ruby-on-rails - 无法使用 Devise 获取新的 Rails 应用程序以显示 sign_in/sign_up 页面

ruby-on-rails - 如何在我的 ConnectionAdapter 回调中使用 Devise 方法?

ruby-on-rails - 在数据库中设计重置密码?

ruby-on-rails - rails 3 habtm只删除关联

ruby-on-rails - 找不到“twitter/bootstrap/responsive.less”

ruby-on-rails - 生成唯一的用户名(omniauth + devise)

ruby-on-rails - devise_mapping.registerable?,devise_mapping.recoverable?....来自哪里?