Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7584937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:02:52+00:00 2026-05-30T19:02:52+00:00

In a view I can do this and the link works fine: <%= link_to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:02:53+00:00Added an answer on May 30, 2026 at 7:02 pm

    [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. This link_to call was in a loop:

    @top_ten_posts each do |post|
    ...
    end
    

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view user control that can post form. This control can be
I know that a view can only be a subview of one superview, so
I know that within a view I can render an action from the same
I can't find any documentation to confirm this, but it appears that you can
I came across this post here (link below) and it states that you can
In the aspx code view , we can code like this: <asp:ListBox runat=server> <asp:ListItem
How can I fix the view of this twitter on my blogger blog. Here
I have a View that can vary significantly, depending on the 'mode' a particular
I have a tool that works perfectly fine in Chrome and FF. But with
Is there an easy way to limit the number of times a view can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.