【问题标题】:Rails raises InvalidURIErrorRails 引发 InvalidURIError
【发布时间】:2011-11-15 09:20:31
【问题描述】:

当我在控制器中使用 redirect_to 'index' 时,我收到以下错误。 -关于 Ruby 1.8.7 和 Rails 3.1.1-

Started GET "/presentations/1" for 127.0.0.1 at Tue Nov 15 01:48:42 +0200 2011
  Processing by PresentationsController#show as HTML
  Parameters: {"id"=>"1"}
Redirected to http://localhost:3000index
Completed 302 Found in 2ms
[2011-11-15 01:48:42] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000index (or bad hostname?)

这就是它在 routes.rb 中的样子

..
get 'about_us' => 'presentations#index', :as => 'about_us'
..
resources :presentations

显然它试图重定向到http://localhost:3000index 而不是http://localhost:3000/presentations/index,但我不明白为什么。如果我使用redirect_to presentations_pathredirect_to :controller => 'presentations', :action => 'index',一切正常,但为什么会这样?我认为如果 redirect_to 使用现有设置引用同一个控制器,我应该能够使用 redirect_to 而不定义任何控制器参数,还是我错了?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    根据您当前的路线文件,您说

    /about_us routes to {:controller => :presentations, :action => :index}
    

    所以,如果你要输入

    redirect_to '/index'
    

    它会工作得很好。在您的控制台中键入 rake:routes 以确保按照您的意愿生成路由。

    另外,http://guides.rubyonrails.org/routing.html 可能是您入门的绝佳资源。

    【讨论】:

    • 我检查了路线,它们没问题,我已经可以按照我说的方式让它工作了。我只是想知道我错过了什么才能以应有的方式工作?我的意思是当我使用redirect_to 'index' 时显然不应该有任何问题,但是有。我不能简单地忽略这样的问题并使用其他解决方案。
    • 啊哈,redirect_to 不只是将动作名称作为参数。结帐api.rubyonrails.org/classes/ActionController/Redirecting.html,你可以看到它响应的所有参数。
    猜你喜欢
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多