【问题标题】:rails root_url redirects me to www.example.comrails root_url 将我重定向到 www.example.com
【发布时间】:2018-03-10 22:20:02
【问题描述】:

当我使用 rails test 执行测试时,我得到了一些非常奇怪的东西。

我正在尝试理解并创建我的第一个控制器测试,但我得到了一些意想不到的结果。

我在控制台中收到此错误:

F

Failure:
FeedbacksControllerTest#test_should_create_resource [/Users/Daniel/GitHub/haeapua-rails/test/controllers/feedbacks_controller_test.rb:15]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/>
Response body: <html><body>You are being <a href="http://www.example.com/">redirected</a>.</body></html>

这是我的测试

test "should create resource" do

  assert_difference 'Feedback.count', 1 do 
    post feedbacks_url, params: { feedback: { email: "me@myemail.com", summary: "my feedback", rating: 5 } }
  end

  assert_response :success
  assert_redirected_to root_url
end

这是我的控制器

class FeedbacksController < ApplicationController

  # GET /feedbacks/new
  def new
    @feedback = Feedback.new
    # @feedback.user = current_user if user_signed_in?
  end

  # POST /feedbacks
  def create
    @feedback = Feedback.new(feedback_params)
    # @feedback.user = current_user if user_signed_in?

    if @feedback.save
      # flash[:info] = "We got it, thanks. Someone will contact you ASAP once we read it"
      redirect_to root_url
    else
      render :new
    end
  end

  private
    def feedback_params
      params.require(:feedback).permit(:email, :summary, :rating)
    end
end

当我在redirect_to 之前的反馈控制器中放置一个“puts root_url”时,我得到了"http://www.example.com/" 的值。我在整个代码中搜索root_url,而我唯一拥有的部分是在控制器和我的路线中root to: 'static_pages#concept'

我正在使用设计,你认为可能是因为这个吗?我有点迷茫,不知道从哪里开始寻找!

【问题讨论】:

  • 你用水豚吗?
  • 是的,我确实安装了 gem。嗯,明天研究一下。

标签: ruby-on-rails debugging model-view-controller ruby-on-rails-5


【解决方案1】:

这是一个奇怪的怪癖,但 rails 有两种类型的 url_helpers 用于路由。 something_url 和 something_path。第一个是绝对路径,http://www.awesome.com/something) 第二个是相对路径(/someurl)。

除非您在测试环境中明确设置了域名,否则请在测试中使用 _path 帮助程序。如果你想使用 _url 帮助器,那么你需要在应用程序的测试环境(config/environments/test.rb)中配置一个基本 url

【讨论】:

  • 我知道 _path 和 _url。但是您的回答并没有解释为什么我默认设置了 example.com。
  • 您可能知道它们,但是您在测试中使用 _url 并使用 assert redirected_to root_url 而不是 root_path,并且您的环境中没有设置域。 stackoverflow.com/a/48865478/793330
  • 你能详细说明这样做有什么问题吗?
【解决方案2】:

也许某些默认配置正在设置此值。 https://github.com/teamcapybara/capybara/blob/master/lib/capybara.rb#L452

Capybara.configure do |config|
  # ...
  config.default_host = "http://www.example.com"
  # ...
end

您必须在项目配置中覆盖此设置。


如果不是水豚,可能是导轨问题?从这个线程https://github.com/rspec/rspec-rails/issues/1275的输入我会尝试这样的事情

# config/environment/test.rb
config.action_controller.default_url_options = {
  host: '0.0.0.0:3000' # or whatever your host is
}

在我找到的线程中:

# putting this into initializer of test framework
[ApplicationController, ActionController::Base].each do |klass|
  klass.class_eval do
    def default_url_options(options = {})
      { :host => "example.com" }.merge(options) # or localhost:8888 or 0.0.0.0:3000 or whatever your server is
    end
  end
end

【讨论】:

  • 我在 m Gemfile 中评论了 Gem,但它仍然这样做。我仍然无法弄清楚这个值是在哪里定义的。它必须在某个地方......
  • 只是确定,你在评论后$ bundle install了吗?
猜你喜欢
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多