在process_action回调之前:authenticate_user!尚未定义

Dia*_*aBG 7 ruby ruby-on-rails devise twilio ngrok

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

我能够在控制台中打开Ngrok并获取他们为我的网址提供的网络ID.当我将网址插入我的浏览器时,我不断收到此错误.我应该到我自己的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
Run Code Online (Sandbox Code Playgroud)

真的不确定是什么问题.当它没有连接到'def reply'时,authenticate_user应该由devise定义.

phi*_*ash 14

Twilio开发者传道者在这里.

看起来这是Rails 5似乎引入的一个问题.如果过滤器尚未在控制器中使用时定义,则会引发错误.这也是在Clearance项目中发现的.

他们的修复方法是将raise: false选项传递给skip_before_filter:

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

end
Run Code Online (Sandbox Code Playgroud)

如果这有帮助,请告诉我.


Pro*_*ton 8

当我使用Devise开发Rails 6应用程序时,我遇到了类似的问题

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
Run Code Online (Sandbox Code Playgroud)

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

Before process_action callback :authenticate_admin! has not been defined
Run Code Online (Sandbox Code Playgroud)

这是我修复它的方法

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

# app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  before_action :authenticate_admin!

end
Run Code Online (Sandbox Code Playgroud)

现在我可以在产品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
Run Code Online (Sandbox Code Playgroud)

如果我不想before_action :authenticate_user!在中定义,另一种选择application_controller是使用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
Run Code Online (Sandbox Code Playgroud)

就这样。

我希望这有帮助

  • 这应该是正确的答案。读完这篇文章后,一切都变得有意义了。因为authenticate_admin是一个设计方法,所以它不是开箱即用的回调。 (3认同)