Solved

Ruby on Rails :  "No route matches controller"

Posted on 2010-11-13
4
2,389 Views
Last Modified: 2012-05-10
I am new to RoR, but I have been struggling with a mysterious error for days.
I am using a standard installation approach on a clean ubuntu system

I created a very simple application, just reading from a mysql table called dogs.
The application was totally generated with scaffold.

rails server -p 3001   #Starts server


When I run, I get the error "No route matches controller"
   http://localhost:3001/dogs  

If I remove all the link_to cells, the application runs and returns data.

If I put back just one line "    <td><%= link_to 'Show', dog %></td>",
 I still get the error complaining about the  ":action=>"destroy" route.


== scaffold ==

rails g scaffold dog dog_id:integer color:string gender:string dog_name:string --skip-migration


===========Generated Source Code=============

<% @dogs.each do |dog| %>
  <tr>
    <td><%= dog.dog_id %></td>
    <td><%= dog.color %></td>
    <td><%= dog.gender %></td>
    <td><%= dog.dog_name %></td>
    <td><%= link_to 'Show', dog %></td>
    <td><%= link_to 'Edit', edit_dog_path(dog) %></td>
    <td><%= link_to 'Destroy', dog, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>

========= ERROR ===========
## No route matches controller ############
Showing /home/ruby/hub2/app/views/dogs/index.html.erb where line #20 raised:
No route matches {:controller=>"dogs", :action=>"destroy", :id=>#<Dog dog_id: 1, color: "Golden", gender: "Female", dog_name: "Daisy">}

Extracted source (around line #20):

17:     <td><%= dog.color %></td>
18:     <td><%= dog.gender %></td>
19:     <td><%= dog.dog_name %></td>
20:     <td><%= link_to 'Show', dog %></td>
21:     <td><%= link_to 'Edit', edit_dog_path(dog) %></td>
22:     <td><%= link_to 'Destroy', dog, :confirm => 'Are you sure?', :method => :delete %></td>
23:   </tr>

========== routes.rb   ==============
Hub2::Application.routes.draw do
  resources :dogs
end


========= rake routes ============
root@ubu-bob:/home/ruby/hub2# rake routes
(in /home/ruby/hub2)
    dogs GET    /dogs(.:format)          {:controller=>"dogs", :action=>"index"}
    dogs POST   /dogs(.:format)          {:controller=>"dogs", :action=>"create"}
 new_dog GET    /dogs/new(.:format)      {:controller=>"dogs", :action=>"new"}
edit_dog GET    /dogs/:id/edit(.:format) {:controller=>"dogs", :action=>"edit"}
     dog GET    /dogs/:id(.:format)      {:controller=>"dogs", :action=>"show"}
     dog PUT    /dogs/:id(.:format)      {:controller=>"dogs", :action=>"update"}
     dog DELETE /dogs/:id(.:format)      {:controller=>"dogs", :action=>"destroy"}


=== Installation ===
 I followed these instructions on a brand new and fully updated version of ubuntu 10.04

 http://castilho.biz/blog/2010/05/08/how-to-install-ruby-on-rails-on-ubuntu-10-04-lucid-lynx/
 How to install Ruby on Rails on Ubuntu 10.04 Lucid Lynx
    Follow these steps to have a fresh installation of Ruby on Rails in Ubuntu 10.04:
   
  sudo su
  apt-get -y install build-essential
  apt-get -y install ruby rdoc libopenssl-ruby
  wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
  tar zxvf rubygems-1.3.7.tgz
  cd rubygems-1.3.7
  ruby setup.rb
  ln -s /usr/bin/gem1.8 /usr/local/bin/gem
  gem install rails  ;# (takes awhile with no output.. )
   
#Installing MySQL gem:
   
  apt-get -y install ruby-dev libmysql-ruby libmysqlclient-dev
  gem install mysql

======
0
Comment
Question by:bobtreu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:ostraaten
ID: 34130998
You are using named routes but these donlt exist or you are using them incorrectly. Instead named routes 'dog' try :controller => 'dogs', :action => 'show', :id => @dog or something. If that works try named routes. Do you have 'resource' in routes.rb
0
 

Author Comment

by:bobtreu
ID: 34131720
If what I have is wrong, then it was generated wrong by Scaffold.   If you can tell me what is wrong, I'd appreciate that.  

You suggested:
 "' try :controller => 'dogs', :action => 'show', :id => @dog "    or something.

The "or something"  throws me off a bit.    Are you suggesting that what scaffold generates is wrong?
I should use this in place of link_to?

You asked about the routes.rb file.   I included that in the original question.  

I also included the output from "rake routes"

Thanks for trying to help.
0
 

Author Comment

by:bobtreu
ID: 34131763
What I tried:   ( both with and without link_to )

<% @dogs.each do |dog| %>
  <tr>
    <td><%= dog.dog_id %></td>
    <td><%= dog.color %></td>
    <td><%= dog.gender %></td>
    <td><%= dog.dog_name %></td>
    <td><=% :controller => 'dogs', :action => 'show', :id => @dog %></td>
    <td><=% link_to :controller => 'dogs', :action => 'show', :id => @dog %></td>
  </tr>
<% end %>


What I see:

Dog       Color       Gender       Dog name                         
1       Golden       Female       Daisy       
 <=% :controller => 'dogs', :action => 'show', :id => @dog %>       
 <=% link_to :controller => 'dogs', :action => 'show', :id => @dog %>
0
 
LVL 10

Accepted Solution

by:
Andrew Doades earned 500 total points
ID: 34182717
Your link's aren't quite right, try the below

<%= link_to "Text you want to show", :controller => 'dogs', :action => 'show', :id => dog.id %>
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question