0

在访问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

我该如何解决这个问题?

4

2 回答 2

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在正确的位置看到测试失败。

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

于 2013-01-13T13:40:55.843 回答
0

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

给定您提供的输出,如果您替换并使用数据库中的有效值rake routes,类似 urllocalhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER>应该根据输出第一行中的规则工作。EXAMPLE_PASS_TYPE_IDENTIFIEREXAMPLE_SERIAL_NUMBER

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

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

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

例如:

root :to => "passbook/index"

如果有passbooks_contoller.rb一个index方法。

最好从文档中了解有关Rails 路由的更多信息。最好通过Rails 教程来更好地理解 Rails 框架。

于 2013-01-13T13:43:12.460 回答