2011-01-30 47 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]+$/