Rails 3.1 - link_to 生成“没有路由匹配”的错误.first 出现错误
在视图中,我可以执行此操作,并且链接工作正常:
<%= link_to "Most popular comment", comment_path( Comment.find(5) ) %>
所以我知道我的路线设置为通过 comment_path()
显示单独的评论记录。但是,当我尝试此操作时:
<%= link_to "Most popular comment", comment_path( @post.comments.order("vote_cnt DESC").first )
我收到“没有路由匹配 {:action=>"show", :controller=>"comments"}
" 错误。但我知道这不是准确的错误描述,因为上面列出的第一个 link_to()
语句有效。我已经确认路由存在 - 从 rake paths
我得到了这个:
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
在 IRB 中,我可以看到我传递给 comment_path()
的两个语句都生成相同的内容class,即“Comment
”:
irb(main):022:0> top_comment = post.comments.order("vote_cnt DESC").first
Comment Load (0.6ms) SELECT "comments".* FROM "comments" WHERE "post_id" = 2 ORDER BY vote_cnt DESC LIMIT 1
=> #<Comment id: 5, heading: nil, body: nil, user_id: 5, created_at: "2012-02-03 01:23:30", updated_at: "2012-02-03 01:23:30",vote_cnt: 0>
irb(main):023:0> top_comment.class
=> Comment(id: integer, heading: string, body: text, user_id: integer, created_at: datetime, updated_at: datetime, vote_cnt: integer)
irb(main):024:0> comment_5 = Comment.find(5)
Comment Load (3.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 5]]
=> #<Comment id: 5, heading: nil, body: nil, user_id: 5, created_at: "2012-02-03 01:23:30", updated_at: "2012-02-03 01:23:30", vote_cnt: 0>
irb(main):025:0> comment_5.class
=> Comment(id: integer, heading: string, body: text, user_id: integer, created_at: datetime, updated_at: datetime, vote_cnt: integer)
如果两个语句生成相同的对象类,那么 link_to()
如何与一个语句一起使用并导致另一个语句发生路由错误,特别是当两者都解析为相同的记录?我尝试过:
<% top_comment = post.comments.order("vote_cnt DESC").first %>
<%= link_to "Most popular comment", comment_path( top_comment )
这会生成相同的“没有路径匹配...”错误。
有什么想法吗?看起来 link_to()
可以很好地处理来自对表的直接查询的记录,但如果通过 ActiveRecord::Relation 操作。为什么?怎么可能呢?
In a view I can do this and the link works fine:
<%= link_to "Most popular comment", comment_path( Comment.find(5) ) %>
So I know that my routes are set up to show an individual comment record via comment_path()
. However when I try this:
<%= link_to "Most popular comment", comment_path( @post.comments.order("vote_cnt DESC").first )
I get a "No route matches {:action=>"show", :controller=>"comments"}
" error. But I know this not an accurate error description because the first link_to()
statement listed above works. I've confirmed the route exists - from rake routes
I get this:
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
In IRB I can see that the two statements I'm passing to comment_path()
both generate the same class, namely "Comment
":
irb(main):022:0> top_comment = post.comments.order("vote_cnt DESC").first
Comment Load (0.6ms) SELECT "comments".* FROM "comments" WHERE "post_id" = 2 ORDER BY vote_cnt DESC LIMIT 1
=> #<Comment id: 5, heading: nil, body: nil, user_id: 5, created_at: "2012-02-03 01:23:30", updated_at: "2012-02-03 01:23:30",vote_cnt: 0>
irb(main):023:0> top_comment.class
=> Comment(id: integer, heading: string, body: text, user_id: integer, created_at: datetime, updated_at: datetime, vote_cnt: integer)
irb(main):024:0> comment_5 = Comment.find(5)
Comment Load (3.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 5]]
=> #<Comment id: 5, heading: nil, body: nil, user_id: 5, created_at: "2012-02-03 01:23:30", updated_at: "2012-02-03 01:23:30", vote_cnt: 0>
irb(main):025:0> comment_5.class
=> Comment(id: integer, heading: string, body: text, user_id: integer, created_at: datetime, updated_at: datetime, vote_cnt: integer)
If both statements generate the same class of object, how can link_to()
work with one and cause a route error on the other, especially when both resolve to the same exact record? I tried:
<% top_comment = post.comments.order("vote_cnt DESC").first %>
<%= link_to "Most popular comment", comment_path( top_comment )
and that generates the same "No path matches..." error.
Any ideas what is going on here? It seems that link_to()
works well with a record that comes from a straight up query against a table, but errors out on that same record if was retrieved via an ActiveRecord::Relation
action. Why? How can that be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这个很奇怪。您是否在视图中检查了
@post.comments.order("vote_cnt DESC").first
返回的内容(通过打印它,或通过debugger
/pry )?
Ok, this one is weird. Have you checked what's returned by
@post.comments.order("vote_cnt DESC").first
in your view (either by printing it, ordebugger
/pry
) ?[将我上面的评论转换为答案]
对不起,伙计们 - 程序员逻辑错误。
我尝试了“rkb”的 ...
first.id
建议,但收到错误消息,提示我无法获取 nil 对象的 ID。此link_to
调用处于循环状态:由于它们是最近的帖子,因此其中一个没有任何评论。呃!我的代码应该首先检查这一点。
我的不好。然而,令人非常沮丧的是 link_to 不会给你一个明确的指示,表明你已经向它传递了一个 nil 对象。 “没有路线匹配...”?!?!?!对于这种情况来说,错误消息非常糟糕!
[converting my comment above into an answer]
Sorry guys - programmer logic error.
I tried "rkb"s suggestion of ...
first.id
, and got an error saying I can't get the ID of a nil object. Thislink_to
call was in a loop:Since they are recent posts, one of them didn't have any comments. Duh! My code should check for that first.
My bad. However, it is very frustrating that link_to won't give you a clear indication that you have passed it a nil object. "No route matches..."?!?!?! Pretty poor error message for the situation!