【发布时间】:2016-07-18 00:55:24
【问题描述】:
我有控制器方法,它使用redirect_to :back 将用户重定向回原始页面。方法如下。
def create
@conversation = Conversation.find(params[:conversation_id])
@message = @conversation.messages.build(message_params)
if @message.save
undeletion_unread
redirect_to :back
flash[:success] = "Message Sent Successfully"
else
redirect_to :back
flash[:danger] = 'Message must be between 1-600 characters'
end
end
我正在尝试为有效和无效的创建方法编写集成测试。下面给出了集成测试。
test "valid creation of a message" do
post user_conversation_messages_path(@user, @conversation), message: {body: "abc"}
assert_equal "Message Sent Successfully", flash[:success]
end
test "invalid creation of a message" do
post user_conversation_messages_path(@user, @conversation), message: {body: " "}
assert_equal "Message must be 1-600 characters", flash[:success]
end
我收到与redirect_to :back 相关的以下错误。
Error:
MessageCreateTest#test_invalid_creation_of_a_message:
ActionController::RedirectBackError: No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].
app/controllers/messages_controller.rb:14:in `create'
test/integration/message_create_test.rb:16:in `block in <class:MessageCreateTest>'
我尝试在 create 方法中使用 HTTP REFERRER(如下所示)而不是 redirect_to :back,但我仍然收到错误。
redirect_to request.env["HTTP_REFERER"]
我不确定我需要更改什么才能通过redirect_to :back 的测试。我曾经遇到过这个问题,最后我改变了路线。但是,在这种情况下使用 redirect_to :back 会容易得多。谷歌搜索问题后,我尝试了不同的建议。它们中的大多数已经过时并且并不真正适用。谢谢
【问题讨论】:
-
@neydroid,我在发布问题之前先检查一下问题。我不确定提出的解决方案到底是什么。此外,答案是 5 岁。轨道和其他方面的许多事情都发生了变化。它可能实际上不起作用
-
您可以发布您的路由器配置以通过适当的示例为您提供答案吗?
标签: ruby-on-rails ruby rspec