【问题标题】:Why is this basic Rails 3 route not working?为什么这条基本的 Rails 3 路线不起作用?
【发布时间】:2010-10-20 12:17:38
【问题描述】:

我刚刚升级到 Rails 3,并决定使用虚拟/沙盒应用程序试一试。奇怪的是我似乎无法获得任何条路线!

这是我的问题:

[cobychapple@shiva:Dev]$rails new TestApp
      create  
      create  README
      create  Rakefile
      ...
[cobychapple@shiva:Dev]$cd TestApp/
[cobychapple@shiva:TestApp]$rails g scaffold widget name:text
      invoke  active_record
      create    db/migrate/20101020115518_create_widgets.rb
      create    app/models/widget.rb
      ...
[cobychapple@shiva:TestApp]$rake db:migrate
(in /Users/cobychapple/Dev/TestApp)
==  CreateWidgets: migrating ==================================================
-- create_table(:widgets)
   -> 0.0015s
==  CreateWidgets: migrated (0.0016s) =========================================

[cobychapple@shiva:TestApp]$rake routes
(in /Users/cobychapple/Dev/TestApp)
    widgets GET    /widgets(.:format)          {:action=>"index", :controller=>"widgets"}
    widgets POST   /widgets(.:format)          {:action=>"create", :controller=>"widgets"}
 new_widget GET    /widgets/new(.:format)      {:action=>"new", :controller=>"widgets"}
edit_widget GET    /widgets/:id/edit(.:format) {:action=>"edit", :controller=>"widgets"}
     widget GET    /widgets/:id(.:format)      {:action=>"show", :controller=>"widgets"}
     widget PUT    /widgets/:id(.:format)      {:action=>"update", :controller=>"widgets"}
     widget DELETE /widgets/:id(.:format)      {:action=>"destroy", :controller=>"widgets"}

[cobychapple@shiva:TestApp]$rails s -d
=> Booting WEBrick
=> Rails 3.0.1 application starting in development on http://0.0.0.0:3000
[cobychapple@shiva:TestApp]$

所以现在我转到我的浏览器并访问http://localhost:3000/widgets

Rails 说: 路由错误:没有路由匹配“/widgets”

我不明白 rake 路由如何显示路由存在,然后服务器将无法匹配它。我用谷歌搜索了一大堆,似乎有几个人在 rails 3 中有路由问题,但没有一个人像我得到的那样明显基本。我敢肯定,我只是错过了一些小事,但我一生都无法弄清楚!有什么建议吗?

我的 routes.rb 文件只是脚手架生成的 + 文档 cmets:

TestApp::Application.routes.draw do
  resources :widgets

  # The priority is based upon order of creation:
  # skip the rest of the comments...
end

【问题讨论】:

  • 你有文件 app/controller/widgets_controller.rb 吗?
  • 我做了和你一样的步骤,而且没有问题。奇怪...
  • 是的... app/controllers/widgets_controller.rb 存在并由脚手架脚本生成。感谢您的检查!

标签: ruby-on-rails ruby-on-rails-3 routes


【解决方案1】:

修改路由后需要重启服务器。 正如您在回答中所说,您已经使用 -d 选项启动了服务器,因此它进入了后台,然后您尝试再次运行它 - 但第一个仍在运行。你应该杀死第一台服务器。

你提到的那个后台进程,不是任何流氓进程,它是你的第一台服务器。

答案是: 1)不要在开发中使用'-d'选项启动服务器(这样以后重启或杀死它会更容易) 2) 更改路由文件(或 config/initializers 中的任何文件)后始终重启服务器

【讨论】:

    【解决方案2】:

    我做了完全相同的事情,它对我有用。我唯一不同的是使用 mysql 数据库。 你为什么不删除应用程序并重新做一遍。我无法想象你错过了什么。

    这些是我的步骤:

    rails new TestApp -d mysql
    cd TestApp/
    rails g scaffold widget name:text 
    rake db:create
    rake db:migrate
    rake routes
    (in /home/gjp/work/TestApp)
        widgets GET    /widgets(.:format)          {:controller=>"widgets", :action=>"index"}
        widgets POST   /widgets(.:format)          {:controller=>"widgets", :action=>"create"}
     new_widget GET    /widgets/new(.:format)      {:controller=>"widgets", :action=>"new"}
    edit_widget GET    /widgets/:id/edit(.:format) {:controller=>"widgets", :action=>"edit"}
         widget GET    /widgets/:id(.:format)      {:controller=>"widgets", :action=>"show"}
         widget PUT    /widgets/:id(.:format)      {:controller=>"widgets", :action=>"update"}
         widget DELETE /widgets/:id(.:format)      {:controller=>"widgets", :action=>"destroy"}
    rails s
    

    然后http://localhost:3000/widgets 工作了。

    【讨论】:

      【解决方案3】:

      看来我已经解决了问题(但我还是不明白!):

      我正在做的是使用 -d(分离模式)运行服务器脚本,这会将您直接返回到控制台。我尝试在没有 -d 选项的情况下运行服务器,以查看服务器活动日志的结果,当我以这种方式运行服务器脚本时,它直接崩溃说端口已在使用中(即使运行分离它没有' t 崩溃?!?!?)。

      重新启动。一切正常,服务器脚本在任一模式下再次正常运行。

      所以我想一定是有一些后台端口绑定问题在重新启动后被清除了。如果将来其他任何人都发生这种情况,我猜你可能有一些流氓进程(MAMP 错误?来自另一个 Rails 应用程序的流氓杂种服务器?)绑定到某个地方的主机端口。检查一下,如果找不到任何东西,请重新启动并查看是否修复。

      耸耸肩

      感谢您的意见,伙计们和/或女孩们。

      【讨论】:

        【解决方案4】:

        在正确运行的开发环境中,不需要重新启动服务器来重新加载路由。

        忽略这一点 - 因为您在分离模式下启动了第二次服务器尝试,所以您没有看到它收到的绑定错误。

        在前台运行服务器通常更容易,这样您就可以看到任何错误。

        如果您确实遇到绑定错误,那么您可以追踪它:

        sudo netstat -an | grep LISTEN | grep 3000
        

        ps aux | grep -i rails
        

        【讨论】:

          猜你喜欢
          • 2015-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-24
          • 2011-03-18
          • 2011-04-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多