hlh
hlh

Reputation: 2072

Why am I seeing "No route matches :action=>"show"" when my show action is defined for that controller?

I have a "Category" resource, and am trying to link to Categories#show in its index.html.erb file using the link_to method and the _path routes. When I view the index in my browser I see the following error:

Routing Error No route matches {:action=>"show", :controller=>"categories"}

But I do have a show action defined in my Categories controller, and removing the URI part of link_to causes the index to display normally without having an exception thrown! I can't figure out where I'm going wrong with the URI.

Here's /categories/index.html.erb:

<h1>My Links</h1>

<% @categories.each do |c| %>
    <h2><%= link_to "#{c.category}", category_path %></h2>
    <p><%= c.description %></p>
<% end %>

<%= link_to "Add new category", new_category_path %>

Here's /categories/show.html.erb:

<h1><%= @category.category %></h1>

<p><%= @category.description %></p>

<p><%= link_to "Back to index", root_path %></p>

Here's /config/routes.rb:

LinkManager::Application.routes.draw do

  resources :categories do
    resources :links
  end

  root :to => 'categories#index'

Here's part of /controllers/categories_controller.rb:

class CategoriesController < ApplicationController

    def index
        respond_with(@categories = Category.all)
    end


    def show
        @category = Category.find(params[:id])
    end

And here's the result of running rake routes (put it on pastebin since I couldn't figure out how to format it nicely here): rake routes

Can anyone spot what's wrong? I've looked at many other similar routing questions here but couldn't get a solution from them. Thank you.

Upvotes: 0

Views: 759

Answers (2)

jdoe
jdoe

Reputation: 15771

Try to replace:

<h2><%= link_to "#{c.category}", category_path %></h2>

with:

<h2><%= link_to "#{c.category}", category_path(c) %></h2>

See? You have to provide a category for category_path.

Or just use the magic:

<h2><%= link_to "#{c.category}", c %></h2>

And by the way, what's the purpose of interpolation here: "#{c.category}"? Wouldn't simply c.category be enough?

Upvotes: 2

Eliaspirante
Eliaspirante

Reputation: 1

Well, i think if you add other route to action show it works, do this:

do this in your browser:

localhost:3000/categories/show

and in your routes.rb:

match "/categories/show/:id" => categories#show

what is the version of our ruby and the version of rails?

Upvotes: 0

Related Questions