2011-01-30 6 views
6

스캐 폴드를 사용하여 레일스 앱을 시작했습니다. 이 앱은 사람들을 기관과 관련시킵니다. 나는컨트롤러 show-scaffold 생성 코드와 일치하는 경로가 없습니다.

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와 설정을 함께 할 수있는 뭔가가 있나요? 모델인지 경로 문제인지 확실하지 않습니다. 또는 내가 생각하지 못한 것. 스캐 폴딩 후 레일 서버를 다시 시작했습니다.

답변

13

표시 및 삭제 링크에서 person 대신 person_path(person)을 사용해보십시오.

편집 : 기본 id 이외의 다른 기본 키를 사용하고있는 것을 확인하지 못했습니다. person_path(person) 대신 person_path(person.pid)을 사용해보세요.

+0

총에 맞습니다. 같은 오류. : – Libby

+0

routes.rb 파일 전체를 게시 할 수 있습니까? –

+0

routes.rb를 포함하도록 수정 된 질문입니다. 응답하는 올바른 방법입니까? (stackoverflow에 익숙하며 아직 시스템을 학습 중입니다) – Libby

1

레일 기본값 ('id')과 다른 pk를 선택 했으므로 대신 모델에이를 사용하도록 지시해야합니다.

class Person < ActiveRecord::Base 

    set_primary_key "pid" 

end 
+1

이제는 더 이상 사용되지 않을 것입니다. self.primary_key = "pid"' – silasjmatson

1

지구상에 무엇이 잘못되었는지를 이해하지 않고 몇 시간 동안 동일한 문제를 해결하기 위해 고심하고 있습니다.

코드는 이전에 작동했지만 스캐 폴드에서 생성 된 것처럼 갑자기 작동을 멈췄습니다. 만 인덱스 작업은 다음 오류와 함께 작동이 중지 :

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

나를 위해 이유는, 내가 (내가 set_primary_key "사용자 이름"을 가지고, 그 아무것도 변경하지 않고 나머지 작업을했다) 다른 ID를하였습니다 아니었지만, 내가 : "test.est"라는 점을 가지고 id를 도입했는데 그게 모든 문제를 일으켰습니다.

그래서 이제부터는 모든 문자열 ID가 (내가 악센트를 허용하는 정규 표현식을 찾을 때까지 (áéíúú ...) :

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