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

时间:2016-12-21 15:22:53

标签: ruby-on-rails ruby 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

真的不确定是什么问题。 当它没有连接到&#39; def回复&#39;和authenticate_user应该由devise定义。

2 个答案:

答案 0 :(得分:9)

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

让我知道这是否有帮助。

答案 1 :(得分:0)

在使用 Devise gem进行身份验证和授权的 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

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

Before process_action callback :authenticate_admin! has not been defined

这是我修复的方式

要在产品控制器中使用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

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

仅此而已。

我希望这会有所帮助