ruby-on-rails - 没有路线匹配 Controller 显示-脚手架生成的代码

标签 ruby-on-rails routing

我使用脚手架启动了一个Rails应用程序。该应用程序将人们与机构联系起来。当我去

http://localhost:3000/people

我收到以下错误:

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

(等等)

如果我删除支架生成的表中的所有“link_to”单元格,则页面加载就很好了。我的应用程序中的所有index.html.erb文件都会发生此错误。

这是我的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 %>

以及我的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

和耙路的结果
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服务器。

最佳答案

尝试在显示和删除链接下使用person_path(person)而不是person

编辑:我没有注意到您使用的是与默认id不同的主键。尝试使用person_path(person.pid)而不是person_path(person)

关于ruby-on-rails - 没有路线匹配 Controller 显示-脚手架生成的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4844300/

相关文章:

ruby-on-rails - 尝试使用命名空间实现模块

ruby-on-rails - 添加一个浅层资源作为另一个资源的成员

routing - 具有任意数量嵌套级别的工兵路线

routing - 将IP范围转发到在Vagrant/VirtualBox中运行的Kubernetes节点

ruby-on-rails - 有没有办法在 Rails 中以编程方式查看(或打印)方法定义?

Mysql2::错误:使用 Rails 的 key '2016' 重复条目 'PRIMARY'

javascript - React Router v4 嵌套匹配参数无法在根级别访问

scala - 为一堆 url 添加前缀

ruby-on-rails - 将 "1.00"添加到所有现有的十进制记录

asp.net - 在 IHTTPHandler 中以编程方式创建 System.Web.UI.Page