Rails: 在执行动作之前的process_action回调中,authenticate_user!未被定义。

17

我正在创建一个使用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定义。


你指的是什么错误?有堆栈跟踪吗? - philnash
错误信息为:“MessagesController#reply 中的 ArgumentError”、“在 process_action 回调之前,authenticate_user! 未被定义”。 - DianaBG
它会突出显示“skip_before_filter:authenticate_user!,:only =>“reply””这一行。 - DianaBG
当我删除那行代码时,会出现以下错误:"MessagesController#reply中的NameError"..."MessagesController::Twilio"未初始化。这突出了以下代码@client = Twilio::REST::Client.new account_sid, auth_token - DianaBG
好的,我还在查看您的第一个错误,但第二个错误听起来像是您没有安装Twilio gem。请将gem 'twilio-ruby'添加到您的Gemfile中,运行bundle install,然后再试一次。 - philnash
你正在使用Rails 4还是5? - philnash
2个回答

30

我是Twilio开发者宣传员。

看起来这是Rails 5引入的问题。如果在控制器中使用过滤器之前没有定义,它将会引发错误。Clearance项目也发现了这个问题

他们的解决方案是向skip_before_filter传递raise: false选项:

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

end

7

当我在使用Rails 6应用程序时,遇到了与Devise宝石进行身份验证和授权相似的问题。

我在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
以下是我如何解决它的方法
要在产品控制器中使用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

现在我可以在产品控制器中使用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

我希望你能看懂这些。

希望这可以帮到你。


2
这应该是正确的答案。读完这个之后整个意思就清楚了。因为authenticate_admin是一个devise方法,它不是开箱即用的回调函数。 - Federico Martinez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接