cola
cola

Reputation: 12466

ActiveRecord::RecordNotFound in PostsController#show clicking a link

<li><%= link_to('More Commented', posts_morecommented_path) %></li>

Error

ActiveRecord::RecordNotFound in PostsController#show

Couldn't find Post with id=morecommented

Request

Parameters:

{"id"=>"morecommented"}

Where am I doing the mistake?

postscontroller#show action

def show      @post = Post.find(params[:id])      ...         end

morecommented.html.erb

<% @moreCommented.each do |t| %>
    <%= link_to t.title, :controller => '/posts', :action => 'show', :id => t.id %><br/>
<% end %>

rake routes

post GET    /posts/:id(.:format)           {:action=>"show", :controller=>"posts"}
....      
posts_morecommented        /posts/morecommented(.:format) {:controller=>"posts", :action=>"morecommented"}

routes.rb:

  resources :posts
  match "posts/:id/categ" => "posts#categ"
  match "posts/:id/tag_posts" => "posts#tag_posts"
  match "posts/searcharchive" => "posts#searcharchive"
  match "posts/morecommented" => "posts#morecommented"

Upvotes: 1

Views: 2653

Answers (2)

Gerry
Gerry

Reputation: 5336

move the match before the resources call

match "posts/morecommented" => "posts#morecommented"
resources :posts

Alternatively you can do

resources :posts do
   get :morecommented, on: :collection
end

Upvotes: 4

Bohdan
Bohdan

Reputation: 8408

Your problem is inside your routes.rb file since routes are matched from the top to the bottom action posts/morecommented matches posts/:id action with params[:id] equal to morecommented One solution as Gerry mentioned is to change order and move the match "posts/morecommented" => "posts#morecommented" before the call resources :posts in your routes.rb file the other one is to set requirements on :id in your posts/:id route.

Upvotes: 2

Related Questions