Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No route matches controller show - scaffold generated code

I started a Rails app using scaffold. The app relates people to institutions. When I go to

http://localhost:3000/people

I get the following error:

No route matches {:controller=>"people", :action=>"show", :id=>#<Person pid: 302, name: 

(and so on)

If I remove all the "link_to" cells in the scaffold-generated table, the page loads just fine. This error happens for all the index.html.erb files in my app.

Here's my people/index.html.erb

<h1>Listing people</h1>

<table>   <tr>  <th></th>
    <th></th>
    <th></th>
    <th></th>   </tr>

<% @people.each do |person| %>   <tr>   <td><%= person.name %></td>
    <td><%= link_to 'Show', person %></td>
    <td><%= link_to 'Edit', edit_person_path(person) %></td>
    <td><%= link_to 'Destroy', person, :confirm => 'Are you sure?', :method
=> :delete %></td>   </tr> <% end %> </table>

<br />

<%= link_to 'New Person', new_person_path %>

And the beginning of my controllers/people.rb

class PeopleController < ApplicationController
  # GET /people
  # GET /people.xml
  def index
    @people = Person.all(:order => "year_grad, name")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @people }
    end
  end

  # GET /people/1
  # GET /people/1.xml
  def show
    @person = Person.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @person }
    end
  end

and the results of rake routes

people GET    /people(.:format)                {:controller=>"people", :action=>"index"}
POST   /people(.:format)                {:controller=>"people", :action=>"create"}
new_person GET    /people/new(.:format)            {:controller=>"people", :action=>"new"}
edit_person GET    /people/:id/edit(.:format)       {:controller=>"people", :action=>"edit"}
person GET    /people/:id(.:format)            {:controller=>"people", :action=>"show"}
PUT    /people/:id(.:format)            {:controller=>"people", :action=>"update"}
DELETE /people/:id(.:format)            {:controller=>"people", :action=>"destroy"}
home_index GET    /home/index(.:format)            {:controller=>"home", :action=>"index"}
root        /(.:format)                      {:controller=>"home", :action=>"index"}

and the migration for people

class CreatePeople < ActiveRecord::Migration
  def self.up
    create_table :people, :id => false, :primary_key => :pid do |t|
      t.integer :pid, :null =>false
      t.string :name
      t.string :degree
      t.integer :phd_area
      t.string :thesis_title
      t.integer :year_grad
      t.integer :instid_phd
      t.integer :year_hired
      t.integer :instid_hired
      t.integer :schoolid_hired
      t.integer :deptid_hired
      t.string :email
      t.string :notes
      t.integer :hire_rankid
      t.integer :tenure_track
      t.integer :prev_instid
      t.integer :prev_rankid
    end
  end

  def self.down
    drop_table :people
  end
end

and here is my routes.rb file (minus the commented lines that scaffolding automatically generates):

IHiring::Application.routes.draw do
  resources :ranks, :departments, :institutions, :schools, :people

  get "home/index"
  root :to => "home#index"

end

Does it have something to do with setting a different primary_key for the table? I'm not sure if it's a model or routes problem. Or something I haven't thought of. I did restart my rails server after scaffolding.

like image 834
Libby Avatar asked Jan 30 '11 17:01

Libby


3 Answers

Try using person_path(person) instead of just person under your Show and Delete links.

Edit: I didn't notice you're using a different primary key than the default id. Try using person_path(person.pid) instead of person_path(person).

like image 137
Dylan Markow Avatar answered Nov 14 '22 22:11

Dylan Markow


since you have chosen a different pk than the rails default ('id'), you will need to tell your model to use that instead.

class Person < ActiveRecord::Base

  set_primary_key "pid"

end
like image 44
MTD Avatar answered Nov 14 '22 22:11

MTD


Even though it was not your case, I've been struggling through the same problem for some hours, not understanding what on earth was wrong.

The code was as generated from scaffold, it had worked before, but it suddenly stopped working. Only the index action stopped working with the following error:

No route matches {:action=>"show", :controller=>"users", :id=>"...."}

The reason for me was not that I had a different id (I had set_primary_key "username", and that made the rest work without changing anything), but that I had introduced an id with a dot: "test.est", and that was causing me all the trouble.

So, from now on, all my string ids will have (until I find a regular expression that accepts accents (áéíóú...):

validates_format_of :username, :with => /^[-A-Za-z0-9]+$/
like image 42
chech Avatar answered Nov 14 '22 23:11

chech