2013-03-12 4 views
0

나는 많은 영화가 감독의 목록을 만들기 위해 노력하고 있어요 (각 영화는 하나의 감독에 속한다) :이 has_many는

class Director < ActiveRecord::Base 
    attr_accessible :director 

    has_many :movies 
end 

class Movies < ActiveRecord::Base 
    attr_accessible :director_id, :title, :rating, :boxoffice 

    belongs_to :director 
end 

내 스키마 모습을 이렇게 :

ActiveRecord::Schema.define(:version => 20130312174246) do 

    create_table "directors", :force => true do |t| 
    t.string "director" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    create_table "movies", :force => true do |t| 
    t.integer "director_id" 
    t.string "title" 
    t.string "rating" 
    t.integer "boxoffice" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    end 

end 

이사 컨트롤러에서

쇼 :

def show 
    @director = Director.find(params[:director_id]) 
    @movies = @director.movies 
    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @director } 
    end 
    end 

내가 궁극적으로 영화의 각각의 등급 및 박스 오피스 $와 함께 이사를 클릭하고 자신이 감독 한 영화를 모두보고 싶습니다. 현재이 오류가 발생합니다. ID가없는 디렉터를 찾을 수 없습니다.

새로운 기능이므로이 문제를 해결하는 방법을 모르겠습니다. 이 작업이 내가하고 싶은 일을위한 올바른 설정입니까? 그렇지 않다면 어떻게 변경해야합니까? 감사!

+0

'params [: director_id]'는 0이어야합니다. – Leito

답변

1

당신은 params[:id]와 감독을 받아야하고,하지와 params[:director_id] 이 시도해보십시오 :

resources :directors do 
    resources :movies 
end 

:

@director = Director.find(params[:id]) 

이사가 중첩 된 경로를 할 만든 영화를 모두 보려면 MovieController에서 모든 작업을 수행하려면 중첩 된 경로 구문을 따라야합니다. 여기에서 찾으십시오 : http://guides.rubyonrails.org/routing.html 또는 좀비 용 레일을 찾으십시오. 2 웹상의 튜토리얼 슬라이드를 찾으십시오. 거기서도 잘 설명되어 있습니다.

+0

좋은 캐치,'레이크 라우트 | grep 'director # show는 당신에게'director GET /directors/ : id (. : format) director # show'를 보여 주어야합니다.': id'는 URL에 의해 주어진 param의 이름입니다. – Leito

+0

나는 단지 추가하고 싶다. 당신이 디렉터 컨트롤러에 있기 때문에, 단지 params [: id] – dorilla

+0

이어야한다. 고마워, 지금까지 정말 도움이되었다! 후속 질문 : 내 디렉터 쇼보기에서 내가 감독의 쇼 링크를 클릭하면 영화 등으로 이동하게하려면 무엇을 포함해야합니까? 지금 내가 쇼를 클릭하면, 그것은 감독의 이름으로 나를 데려 간다. – bork121