Ruby on rails Rails:Before process\u action回调:authenticate\u user!尚未定义
我正在创建一个包含Desive的rails应用程序。 我正在尝试使用Ngrok将Twilio消息添加到我的站点,我使用了以下教程: 我能够在控制台中打开Ngrok并获得他们为我的url提供的web id。 当我将url插入浏览器时,我一直遇到这个错误。我应该访问我自己的rails本地应用程序。不知道怎么了 我在为ngrok制作的消息控制器中添加的内容:Ruby on rails Rails:Before process\u action回调:authenticate\u user!尚未定义,ruby-on-rails,ruby,devise,twilio,ngrok,Ruby On Rails,Ruby,Devise,Twilio,Ngrok,我正在创建一个包含Desive的rails应用程序。 我正在尝试使用Ngrok将Twilio消息添加到我的站点,我使用了以下教程: 我能够在控制台中打开Ngrok并获得他们为我的url提供的web id。 当我将url插入浏览器时,我一直遇到这个错误。我应该访问我自己的rails本地应用程序。不知道怎么了 我在为ngrok制作的消息控制器中添加的内容: class MessagesController < ApplicationController skip_before_filte
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
class messages控制器“回复”
def回复
消息体=参数[“体”]
from_number=params[“from”]
后备箱
sms=@client.messages.create(
发件人:Rails.application.secrets.twilio_编号,
致:从_编号,
身体:“你好,谢谢你发短信给我。你的号码是#{from#u number}。”
)
#twilio需要对此请求的HTTP响应
结束
私有的
def防尘套
帐户\u sid=Rails.application.secrets.twilio\u sid
auth_token=Rails.application.secrets.twilio_token
@client=Twilio::REST::client.new account\u sid,auth\u令牌
结束
结束
真的不知道怎么了。
当其未连接到“def reply”和authenticate_时,用户应由designe定义 这里是Twilio开发者福音传道者 这似乎是Rails 5引入的一个问题。如果筛选器在控制器中使用时尚未定义,则会引发错误 将
raise:false
选项传递到skip\u before\u filter
:
class MessagesController < ApplicationController
skip_before_filter :verify_authenticity_token
skip_before_filter :authenticate_user!, :only => "reply", :raise => false
end
# app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_admin!
end
class messages控制器“回复”,:raise=>false
结束
让我知道这是否有帮助。我在使用designgem开发Rails 6应用程序进行身份验证和授权时遇到了类似的问题 我在操作之前添加了一个
skip\u:authenticate\u admin!,仅:[:索引,:显示]
到产品控制器
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
以下是我如何修复它的方法:
要使用skip\u-before\u操作:authenticate\u-admin!,仅:[:index,:show]
在产品控制器中,我首先需要定义,然后才能执行操作:验证用户在应用程序\u控制器中的code>:
class MessagesController < ApplicationController
skip_before_filter :verify_authenticity_token
skip_before_filter :authenticate_user!, :only => "reply", :raise => false
end
# app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_admin!
end
另一种选择是,如果我不想在\u操作之前定义:authenticate\u user应用程序中的code>控制器
将在执行以下操作之前使用:authenticate\u admin!,除了:[:索引,:显示]
:
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
class ProductsController
就这些
我希望这有帮助你说的错误是什么意思?是否存在堆栈跟踪?错误为“处理前MessagesController中的ArgumentError#reply”“操作回调:验证#用户!尚未定义”,并突出显示行“筛选前跳过:#验证#用户!”:仅=>“reply”“。当我删除该行时,我得到此错误:“MessagesController中的NameError#reply”。。。“未初始化的常量消息控制器::Twilio。这突出显示了代码“``@client=Twilio::REST::client.new account\u sid,auth\u token```好的,仍在查看第一个错误,但第二个错误听起来好像您还没有安装Twilio gem。将gem'twilio ruby'
添加到您的gem文件中,运行bundle安装
并重试。