2011-01-30 49 views
6

我使用腳手架啓動了Rails應用程序。該應用將人們與機構聯繫起來。當我去沒有路線匹配控制器顯示 - 腳手架生成的代碼

http://localhost:3000/people

我收到以下錯誤:

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

(等)

如果我刪除了腳手架生成表中的所有「的link_to」細胞,頁面加載就好了。這個錯誤發生在我應用程序中的所有index.html.erb文件中。

這裏是我的人/ 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 %> 

我的控制器/ 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 

的開始和耙路線的結果

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"} 

和人的遷移

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 

這裏是我的routes.rb文件(減去註釋行是腳手架自動生成):

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

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

end 

它是否有東西做設置不同的primary_key爲表?我不確定它是模型還是路線問題。或者我沒有想到的東西。腳手架後我重新啓動了我的Rails服務器。

回答

13

在您的顯示和刪除鏈接下嘗試使用person_path(person)而不是person

編輯:我沒有注意到您使用的是與默認的id不同的主鍵。嘗試使用person_path(person.pid)而不是person_path(person)

+0

給它一個鏡頭。同樣的錯誤。 :( – Libby 2011-01-30 19:10:44

+0

你可以發佈你的整個的routes.rb文件? – 2011-01-30 19:12:59

1

由於您選擇了與rails默認值('id')不同的pk,因此您需要告訴您的模型使用它。

class Person < ActiveRecord::Base 

    set_primary_key "pid" 

end 
1

即使它不是你的情況,我一直在努力通過同樣的問題幾個小時,不明白究竟什麼是錯的。

該代碼是從腳手架生成的,它曾經工作過,但它突然停止工作。只有指數動作停止,錯誤如下工作:

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

原因我並不是說我有一個不同的ID(我不得不set_primary_key「用戶名」,這讓其餘的工作,而無需改變任何東西),但我已經引入了一個ID與點:「test.est」,這是我所有的麻煩。因此,從現在開始,我所有的字符串id都會有(直到我找到一個接受重音的正則表達式(áéíóú...):

validates_format_of :username, :with => /^[-A-Za-z0-9]+$/