【问题标题】:Rails - Skip Devise Authentication Not WorkingRails - 跳过设计身份验证不起作用
【发布时间】:2016-12-16 18:56:27
【问题描述】:

我正在尝试为我的订阅控制器跳过设计身份验证,但无济于事。我的注册控制器和会话控制器 skip_before_filter 都可以工作,但我最近添加的订阅控制器不能工作。我做错了什么?

class V1::SubscriptionsController < ApplicationController
skip_before_filter :authenticate_user!
skip_before_filter :verify_authenticity_token, if: :json_request?


def create

    @subscription = Subscription.new(subscription_params)

    if @subscription.save 
        render json: @subscription, status: :created
    else
        render json: @subscription.errors, status: :unprocessable_entity
    end
end

private

def subscription_params
    params.require(:subscription).permit(:subscription_email)
end
end

我的应用程序控制器

class ApplicationController < ActionController::API
  include CanCan::ControllerAdditions
  include ActionController::RespondWith
  include ActionController::Serialization

  acts_as_token_authentication_handler_for User

  rescue_from CanCan::AccessDenied do |exception|
    respond_to do |format|
      format.json { render nothing: true, status: :forbidden }

    end
  end
end

还有我的路线

Rails.application.routes.draw do

devise_for :users, skip: [:sessions, :registrations, :passwords]

  api_version(module: "V1", header: { name: "Accept", value: "application/vnd.rolotext.json; version=1" }) do
    devise_scope :user do
      post 'registrations' => 'registrations#create'


      post 'sessions' => 'sessions#create'
      delete 'sessions' => 'sessions#destroy'

      post 'passwords' => 'passwords#create'
      patch 'passwords' => 'passwords#update'
      post   'passwords/check_email' => 'passwords#check_email'
    end

    get '/me' => "users#me"
    patch '/me' => 'users#update'
    delete '/me' => 'users#destroy'


    resources :cards, except: [ :new, :edit]
    patch '/logos' => 'logos#update'
    resources :logos, except: [ :new, :edit, :destroy ]
    resources :uploads
    post '/subscriptions' => "subscriptions#create"

  end
end

任何帮助将不胜感激

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 devise


    【解决方案1】:

    如果我想从身份验证中排除某些页面,我通常会在我的用户控制器中添加它:

    before_filter :authenticate_user!, except: [:sessions, :registrations, :passwords]

    它应该排除在 except.. 中的页面的身份验证。 希望对您有所帮助。

    【讨论】:

    • 我试过了,不幸的是它不起作用。正如我之前提到的,其他控制器在跳过身份验证方面没有问题,并且它们没有在 authenticate_user 中列为异常!在过滤器之前。
    • 最后想一想我正在考虑尝试将该异常添加到应用程序控制器中......
    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多