【问题标题】:Rails: Before process_action callback :authenticate_user! has not been definedRails:在 process_action 回调之前:authenticate_user!尚未定义
【发布时间】:2017-05-07 01:54:24
【问题描述】:

我正在创建一个包含 devise 的 rails 应用程序。 我正在尝试使用 Ngrok 将 Twilio 消息添加到我的网站,我使用了本教程: https://www.twilio.com/blog/2016/04/receive-and-reply-to-sms-in-rails.html

我能够在控制台中打开 Ngrok 并获取他们为我的 url 提供的 web-id。 当我将 url 插入浏览器时,我不断收到此错误。我应该访问我自己的 rails 本地应用程序。不知道出了什么问题。

我在为 ngrok 制作的消息控制器中添加的内容:

class MessagesController < ApplicationController
  skip_before_filter :verify_authenticity_token 
  skip_before_filter :authenticate_user!, :only => "reply"

def reply
   message_body = params["Body"]
   from_number = params["From"]
   boot_twilio
   sms = @client.messages.create(
     from: Rails.application.secrets.twilio_number,
     to: from_number,
     body: "Hello there, thanks for texting me. Your number is #{from_number}."
  )
  #twilio expects a HTTP response to this request
end


private
 def boot_twilio
   account_sid = Rails.application.secrets.twilio_sid
   auth_token = Rails.application.secrets.twilio_token
   @client = Twilio::REST::Client.new account_sid, auth_token
 end
end

真的不确定出了什么问题。 当它没有连接到“def reply”时,authenticate_user 应该由 devise 定义。

【问题讨论】:

  • 你的意思是什么错误?有堆栈跟踪吗?
  • 错误是“ArgumentError in MessagesController#reply”“在 process_action 回调之前:authenticate_user!尚未定义”
  • 并突出显示“skip_before_filter :authenticate_user!, :only => "reply"" 行
  • 当我删除该行时,我收到此错误:“MessagesController#reply 中的 NameError”...“未初始化的常量 MessagesController::Twilio”。这突出了代码``` @client = Twilio::REST::Client.new account_sid, auth_token```
  • 好的,仍在检查您的第一个错误,但第二个错误听起来您还没有安装 Twilio gem。将 gem 'twilio-ruby' 添加到您的 Gemfile,运行 bundle install 并再试一次。

标签: ruby-on-rails ruby devise twilio ngrok


【解决方案1】:

这里是 Twilio 开发者宣传员。

看起来这是 Rails 5 引入的问题。如果在控制器中使用过滤器时尚未定义过滤器,则会引发错误。 This was discovered in the Clearance project too.

Their fix 是将raise: false 选项传递给skip_before_filter

class MessagesController < ApplicationController
  skip_before_filter :verify_authenticity_token 
  skip_before_filter :authenticate_user!, :only => "reply", :raise => false

end

【讨论】:

    【解决方案2】:

    我在使用 Devise gem 进行身份验证和授权的 Rails 6 应用程序时遇到了类似的问题。

    我在 Products 控制器中添加了skip_before_action :authenticate_admin!, only: [:index, :show]

    class ProductsController < ApplicationController
      before_action :set_product, only: [:show, :edit, :update, :destroy]
      skip_before_action :authenticate_admin!, only: [:index, :show]
    
      def index
        @products = Product.all
      end
      .
      .
      .
    end
    

    当我访问 产品 页面时,它抛出了以下错误:

    Before process_action callback :authenticate_admin! has not been defined
    

    这是我修复它的方法

    要在 Products 控制器中使用skip_before_action :authenticate_admin!, only: [:index, :show],我首先需要在application_controller 中定义before_action :authenticate_user!

    # app/controllers/application_controller.rb:
    
    class ApplicationController < ActionController::Base
    
      protect_from_forgery with: :exception
    
      before_action :authenticate_admin!
    
    end
    

    现在我可以在 Products 控制器中使用skip_before_action :authenticate_admin!, only: [:index, :show]

    class ProductsController < ApplicationController
      before_action :set_product, only: [:show, :edit, :update, :destroy]
      skip_before_action :authenticate_admin!, only: [:index, :show]
    
      def index
        @products = Product.all
      end
      .
      .
      .
    end
    

    如果我不想在application_controller 中定义before_action :authenticate_user!,另一种方法是使用before_action :authenticate_admin!, except: [:index, :show]

    class ProductsController < ApplicationController
      before_action :set_product, only: [:show, :edit, :update, :destroy]
      before_action :authenticate_admin!, except: [:index, :show]
    
      def index
        @products = Product.all
      end
      .
      .
      .
    end
    

    就是这样。

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 2016-09-28
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多