Sam Gruse
Sam Gruse

Reputation: 488

Action Controller error. Url Generation error. No route matches

I'm working through the "Ruby on rails 3 essential training" on lynda.com and am having an issue while generating my server. So far I have a subjects_controller.rb, linked to my views folder, to the file list.html.erb. My error when trying to start the server is:

No route matches {:action=>"show", :controller="subjects", :id=>1}

In my list.html.erb file I have written the code:

<td class="actions">
  <%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
  <%= link_to("Edit", '#', :class => 'action edit') %>
  <%= link_to("Delete", '#', :class => 'action delete') %>
</td>

My subjects_controller.rb looks like:

class SubjectsController < ApplicationController
  def list 
    @subjects = Subject.order("subjects.position ASC")
  end
end

I have double checked to make sure I have everything written the same as the instructor but there seems to be a missing link. Any ideas? If I totally cut out the action:

<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>

Then the server starts up. There must be a problem here but I'm not sure what it is. Also when the instructor inputs link_to on his text editor, the txt turns a different color and mine does not. Same thing with his "@" instance variable. Mine doesn't change color. Not sure if this means anything either. Thanks for any input!

Here is my config/routes.rb file:

Rails.application.routes.draw do
  root :to => "demo#index"
  get 'demo/index'
  get 'demo/hello'
  get 'demo/other_hello'
  get 'subjects/list'
end

Upvotes: 0

Views: 5259

Answers (1)

MrTheWalrus
MrTheWalrus

Reputation: 9700

Short version: The error message is telling you exactly what is wrong. You have no route that matches, because while your action is named list, your link specifies :action => 'show'.

Longer version: The second argument to the link_to helper is supposed to tell Rails what URL to generate for the link, usually by specifying one of your routes by name, but sometimes (as in this case), by specifying the action (and optionally the controller). You're specifying the action show. The subjects controller is implied. Therefore, Rails is trying to find a route (in the ones defined in your routes.rb) that GETs the SubjectsController#show action. However, as you can see from your routes.rb, you only define one route on the SubjectsController, and that's list.

If you're ever confused about what routes you have or what their names are, you can use the rake routes task to list them all out in a nice readable format.

Edit to respond to followup question:

The instructor is telling me that when you generate a controller and action that its supposed to add a route to the routes.rb folder. This worked for me earlier but when creating these actions that I'm having trouble with now, it didn't generate anything in the routes.rb folder. Do you know why that is?

When your instructor says 'generate', they probably mean 'use the rails generate command'. When you use the generator to create a controller and specify the actions in it, the it will also add those actions to the routes file. If, on the other hand, you write the action into an existing controller (including using the generator for the controller but not specifying actions), or create the controller file yourself, you'll have to update the routes file manually. If you are using the generator and specifying actions and aren't getting updated routes, I'm not sure what's going on.

Personally, I prefer to write my routes by hand anyway - the generator often doesn't get them exactly right.

Upvotes: 1

Related Questions