Rails:在 process_action 回调之前:authenticate_user!尚未定义
Rails: Before process_action callback :authenticate_user! has not been defined
我正在创建一个包含设计的 rails 应用程序。
我正在尝试使用 Ngrok 将 Twilio 消息传递添加到我的站点,我使用了本教程:
https://www.twilio.com/blog/2016/04/receive-and-reply-to-sms-in-rails.html
我能够在控制台中打开 Ngrok 并获取他们为我的 url 提供的网络 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 时应该由设计定义。
这里是 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
当我使用 Devise [=57] 开发 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
当我访问 Products 页面时出现以下错误:
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
就这些了。
希望对您有所帮助
我正在创建一个包含设计的 rails 应用程序。 我正在尝试使用 Ngrok 将 Twilio 消息传递添加到我的站点,我使用了本教程: https://www.twilio.com/blog/2016/04/receive-and-reply-to-sms-in-rails.html
我能够在控制台中打开 Ngrok 并获取他们为我的 url 提供的网络 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 时应该由设计定义。
这里是 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
当我使用 Devise [=57] 开发 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
当我访问 Products 页面时出现以下错误:
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
就这些了。
希望对您有所帮助