【问题标题】:rake route - No route matches [GET] "/" Errorrake route - 没有路由匹配 [GET] "/" 错误
【发布时间】:2013-01-13 13:21:37
【问题描述】:

在访问localhost:3000 时,我得到了典型的:

没有路线匹配 [GET] "/"

我不想看到那个错误屏幕。我已经尝试使用localhost:3000/passbook/v1/passes/,但仍然没有,所以我输入了:

rake routes

得到这个输出:

passbook GET    /passbook/v1/passes/:pass_type_identifier/:serial_number(.:format)                                           passbook/passes#show {:pass_type_identifier=>/([\w\d]\.?)+/}
             GET    /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier(.:format)                passbook/registrations#index {:pass_type_identifier=>/([\w\d]\.?)+/}
             POST   /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#create {:pass_type_identifier=>/([\w\d]\.?)+/}
             DELETE /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#destroy {:pass_type_identifier=>/([\w\d]\.?)+/}
passbook_log POST   /passbook/v1/log(.:format)                                                                                   passbook/logs#create

我该如何解决这个问题?

【问题讨论】:

  • 添加这一行:root :to => "passbook/passes#show"

标签: ruby-on-rails routes


【解决方案1】:

你还没有告诉 Rails 你的根路径是什么。你可以这样说

root to: "passes#show"

在 config/routes.rb 中。之后,您应该会在 rake routes 输出中看到这一点:

root      /      passes#show

那么它应该可以工作了!


补充提示

这可能超出了您的问题的范围,但是按照测试驱动的方法,您应该首先编写规范:

describe "custom routing" do
    it "shows the root page" do
        get("/").should route_to("passes#show")
    end
end

为此使用RSpec。在您的控制台中运行测试在编写路由本身之前通过执行$ rspec 并在正确的位置看到测试失败。

然后,实现上面的路由,再次运行测试——它应该可以工作了。这样,您可以确保您编写的代码刚好满足您的要求,并且您编写的代码确实可以让您访问根路径。

【讨论】:

    【解决方案2】:

    这是您从其他人那里继承的 Rails 应用程序吗?

    鉴于您提供的rake routes 输出,如果您将EXAMPLE_PASS_TYPE_IDENTIFIEREXAMPLE_SERIAL_NUMBER 替换为数据库中的有效值,则localhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER> 之类的URL 应该根据输出第一行中的规则起作用。

    localhost:3000 的错误消息No route matches [GET] "/ 是因为在rake routes 输出中没有root 的映射。

    您可以修改config/routes.rb 文件并在文件底部添加以下行:

    root :to => "<controller_name>#<action_name>"

    例如:

    root :to => "passbook/index"
    

    如果有 passbooks_contoller.rbindex 方法。

    最好了解有关rails routing from the documentation 的更多信息。最好通过the Rails Tutorial 更好地了解rails 框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多