新手问题 [求助] rails 路由混乱了

kikyous · 2012年07月28日 · 最后由 zw963 回复于 2012年07月28日 · 3552 次阅读
    messages GET    /messages(.:format)                    messages#index
             POST   /messages(.:format)                    messages#create
 new_message GET    /messages/new(.:format)                messages#new
edit_message GET    /messages/:id/edit(.:format)           messages#edit
     message GET    /messages/:id(.:format)                messages#show
             PUT    /messages/:id(.:format)                messages#update
             DELETE /messages/:id(.:format)                messages#destroy
             GET    /messages/archive/:id(.:format)        messages#archive
       notes GET    /notes(.:format)                       notes#index
             POST   /notes(.:format)                       notes#create
    new_note GET    /notes/new(.:format)                   notes#new
   edit_note GET    /notes/:id/edit(.:format)              notes#edit
        note GET    /notes/:id(.:format)                   notes#show
             PUT    /notes/:id(.:format)                   notes#update
             DELETE /notes/:id(.:format)                   notes#destroy
             GET    /notes/archive/:id(.:format)           notes#archive

        root        /                                      home#main
                    /:controller(/:action(/:id))(.:format) :controller#:action

但是访问http://localhost:3000/messages/10

出错 No route matches {:action=>"edit", :controller=>"notes", :id=>nil}

你确定你的message#show相关部分没有重定向逻辑或调用edit_note_path什么的?还有你要先查看日志,确定请求是直接访问的messages#show还是直接报错。

如果没有你可以去给 rails 提 issue 了。。

#1 楼 @hooopo

<%= link_to 'Edit', edit_note_path(@note) %> |
<%= link_to 'Back', notes_path %>

后面这点删除就好了,但是这个应该不影响啊

#2 楼 @kikyous 可以贴一下这个请求的完整日志么

Started GET "/messages/1" for 127.0.0.1 at 2012-07-28 16:06:57 +0800
Processing by MessagesController#show as HTML
  Parameters: {"id"=>"1"}
  [1m[36mUser Load (0.3ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1[0m
  [1m[35mActsAsMessageable::Message Load (0.4ms)[0m  SELECT "messages".* FROM "messages" WHERE "messages"."id" = 1 AND ((sent_messageable_type = 'User' and
 sent_messageable_id = 1 and
 sender_delete = 'f' and sender_permanent_delete = 'f') or
 (received_messageable_type = 'User' and
 received_messageable_id = 1 and
 recipient_delete = 'f' and recipient_permanent_delete = 'f')) ORDER BY created_at desc LIMIT 1
  [1m[36mActsAsMessageable::Message Load (0.4ms)[0m  [1mSELECT "messages".* FROM "messages" WHERE (messages.id = 1 or messages.ancestry like '1/%' or messages.ancestry = '1') ORDER BY created_at desc[0m
  Rendered messages/show.html.erb (2.7ms)
Completed 500 Internal Server Error in 10ms

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"notes", :id=>nil}):
  app/views/messages/show.html.erb:18:in `_app_views_messages_show_html_erb__799486363__610448278'


  Rendered /var/lib/gems/1.9.1/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)

#4 楼 @kikyous 首先路由匹配是正确的:

Started GET "/messages/1" for 127.0.0.1 at 2012-07-28 16:06:57 +0800
Processing by MessagesController#show as HTML
  Parameters: {"id"=>"1"}

然后,看具体错误:

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"notes", :id=>nil}):
  app/views/messages/show.html.erb:18:in `_app_views_messages_show_html_erb__799486363__610448278'

是在 app/views/messages/show.html.erb:18 这个文件的第 18 行,可以贴一下代码么

不懂你的 rake routes 最后一行怎么回事?

#6 楼 @hlcfan

<%= link_to 'Edit', edit_note_path(@note) %> |
<%= link_to 'Back', notes_path %>

应该就是这两句

#7 楼 @kikyous 你看看@note这个变量的 id 是什么,如果不存在就说明是这里的问题

这种问题,建议楼主先看看我最近发的那个帖子吧。

如果连 RESTful 都搞不清楚,routes 你绝对是越看越糊涂...

需要 登录 后方可回复, 如果你还没有账号请 注册新账号