Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 没有路由匹配控制器显示 – 脚手架生成的代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用脚手架开始了一个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服务器.

解决方法

尝试使用person_path(person)而不只是使用Show和delete链接下的person.

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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 没有路由匹配控制器显示 – 脚手架生成的代码全部内容,希望文章能够帮你解决ruby-on-rails – 没有路由匹配控制器显示 – 脚手架生成的代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。