【问题标题】:No route matches controller show - scaffold generated code没有路由匹配控制器显示 - 脚手架生成的代码
【发布时间】:2011-01-30 17:22:18
【问题描述】:

我使用脚手架启动了一个 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

以及 rake 路由的结果

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 服务器。

【问题讨论】:

    标签: ruby-on-rails routing


    【解决方案1】:

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

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

    【讨论】:

    • 你能发布你的整个 routes.rb 文件吗?
    • 当然。编辑问题以包括 routes.rb。这是正确的回应方式吗? (我是 stackoverflow 的新手,还在学习这个系统)
    • 是的,太好了。不过,请查看我上面的更新答案——我认为这与使用 pid 而不是 id 有关。
    【解决方案2】:

    由于您选择了与 rails 默认值 ('id') 不同的 pk,因此您需要告诉您的模型改为使用它。

    class Person < ActiveRecord::Base
    
      set_primary_key "pid"
    
    end
    

    【讨论】:

    • 现在已弃用。你会想要使用self.primary_key ="pid"
    【解决方案3】:

    尽管不是你的情况,但我已经为同一个问题苦苦挣扎了几个小时,不明白到底出了什么问题。

    代码是从脚手架生成的,它以前可以工作,但突然停止工作。只有索引操作停止工作并出现以下错误:

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

    我的原因不是我有一个不同的 id(我有 set_primary_key "username",这使得其余的工作没有改变任何东西),而是我引入了一个带点的 id:“test.est”,这给我带来了麻烦。

    所以,从现在开始,我所有的字符串 id 都将拥有(直到我找到一个接受重音符号的正则表达式 (áéíóú...):

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

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 2011-05-11
      相关资源
      最近更新 更多