2011-11-29 2 views
0

나는 티켓이있는 프로젝트를 가지고 있으며, 여기에는 의견이 있습니다. 모델 연관을위한 코드가 정확하다고 나는 믿는다. 나는 버튼을 클릭하기 전에레일 3 : 3 모델 및 라우팅 오류

class Comment < ActiveRecord::Base 
    belongs_to :ticket, :dependent => :destroy 
    belongs_to :project 
end 

class Ticket < ActiveRecord::Base 
    belongs_to :project 
    has_many :comments, :dependent => :destroy 
end 

class Project < ActiveRecord::Base 
    has_many :tickets 
    has_many :comments, :through => :tickets 
end 

, 경로는 다음과 같습니다

http://localhost:3000/projects/7/tickets/16 

것은 나는 내가 그것을 읽고 어떻게해야합니까 어떻게

http://localhost:3000/tickets/16/comments 

에, 경로 변경 제출 버튼을 클릭하면 :

http://localhost:3000/projects/7/tickets/16/ 

tickets/:id보기에 표시 할 새 댓글? 이 컨트롤러를 사용

ActiveRecord::RecordNotFound in CommentsController#create 
Couldn't find Project without an ID 

:

내가 제출 및 티켓 개체에 대한 의견을 작성, 그것은 활성 기록 오류를 제공 레일에

def create 
    @project = Project.find(params[:project_id]) 
    @ticket = @project.tickets.find(params[:ticket_id]) 
    @comment = @ticket.comments.new(params[:comment]) 
    if @comment.save 
    redirect_to project_ticket_path(@project, @ticket) 
    else 
    end 
end 

새로운하지만이어야한다 모델 연관 또는 라우팅과 함께 ...

내 라우팅 링크 ...

Kaledoscope:ticket_taker Evolution$ rake routes 
    project_tickets GET /projects/:project_id/tickets(.:format)   {:action=>"index", :controller=>"tickets"} 
        POST /projects/:project_id/tickets(.:format)   {:action=>"create", :controller=>"tickets"} 
new_project_ticket GET /projects/:project_id/tickets/new(.:format)  {:action=>"new", :controller=>"tickets"} 
edit_project_ticket GET /projects/:project_id/tickets/:id/edit(.:format) {:action=>"edit", :controller=>"tickets"} 
    project_ticket GET /projects/:project_id/tickets/:id(.:format)  {:action=>"show", :controller=>"tickets"} 
        PUT /projects/:project_id/tickets/:id(.:format)  {:action=>"update", :controller=>"tickets"} 
        DELETE /projects/:project_id/tickets/:id(.:format)  {:action=>"destroy", :controller=>"tickets"} 
      projects GET /projects(.:format)        {:action=>"index", :controller=>"projects"} 
        POST /projects(.:format)        {:action=>"create", :controller=>"projects"} 
     new_project GET /projects/new(.:format)       {:action=>"new", :controller=>"projects"} 
     edit_project GET /projects/:id/edit(.:format)      {:action=>"edit", :controller=>"projects"} 
      project GET /projects/:id(.:format)       {:action=>"show", :controller=>"projects"} 
        PUT /projects/:id(.:format)       {:action=>"update", :controller=>"projects"} 
        DELETE /projects/:id(.:format)       {:action=>"destroy", :controller=>"projects"} 
    ticket_comments GET /tickets/:ticket_id/comments(.:format)   {:action=>"index", :controller=>"comments"} 
        POST /tickets/:ticket_id/comments(.:format)   {:action=>"create", :controller=>"comments"} 
new_ticket_comment GET /tickets/:ticket_id/comments/new(.:format)  {:action=>"new", :controller=>"comments"} 
edit_ticket_comment GET /tickets/:ticket_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"} 
    ticket_comment GET /tickets/:ticket_id/comments/:id(.:format)  {:action=>"show", :controller=>"comments"} 
        PUT /tickets/:ticket_id/comments/:id(.:format)  {:action=>"update", :controller=>"comments"} 
        DELETE /tickets/:ticket_id/comments/:id(.:format)  {:action=>"destroy", :controller=>"comments"} 
      tickets GET /tickets(.:format)        {:action=>"index", :controller=>"tickets"} 
        POST /tickets(.:format)        {:action=>"create", :controller=>"tickets"} 
     new_ticket GET /tickets/new(.:format)       {:action=>"new", :controller=>"tickets"} 
     edit_ticket GET /tickets/:id/edit(.:format)      {:action=>"edit", :controller=>"tickets"} 
      ticket GET /tickets/:id(.:format)       {:action=>"show", :controller=>"tickets"} 
        PUT /tickets/:id(.:format)       {:action=>"update", :controller=>"tickets"} 
        DELETE /tickets/:id(.:format)       {:action=>"destroy", :controller=>"tickets"} 
       root  /(.:format)          {:controller=>"projects", :action=>"index"} 
+0

프로젝트에서 '레이크 경로'를 실행하십시오. 너는 무엇을 얻 느냐? – cocoahero

답변

0

컨트롤러에서 약간의 변경을해야합니다. @ticket.comments.new이있는 곳에 @ticket.comments.build(params[:comment])이 있어야합니다. 예를 들어 :

def create 
    @project = Project.find(params[:project_id]) 
    @ticket = @project.tickets.find(params[:ticket_id]) 
    @comment = @ticket.comments.build(params[:comment]) 
    if @comment.save 
    redirect_to project_ticket_path(@project, @ticket) 
    end 
end 

빌드는 부모의 idticket_id 세트 새로운 코멘트를 구축 할 것입니다. new을 수행하는 경우 ticket_id이 올바르게 설정되지 않으므로 @ticket@comment이 연결되지 않기 때문에 지정된 경로로 리디렉션 할 수 없습니다. 레일 문서에서

:

collection.build(attributes = {}) 반환 속성을 인스턴스화 및 외부 키를 통해이 개체에 연결된하지만 아직 저장되지 않은 한 컬렉션 타입의 하나 또는 그 이상의 새로운 객체.

+0

예, 빌드 방법을 보았습니다. 두 모델에 대한 연관성이 있습니까? – nick

+0

예, 내 업데이트를 참조하십시오. –

+0

감사합니다. Sean! 나는 중첩 된 라우팅을 위해 project_ticket_comments_path와 같은 경로를 제공하는 둘 이상의 레벨을 사용했다. 그 중첩 유형을 수행하지 않는 솔루션을 찾고 있습니다. 내 모델 연관을 확인해야한다는 것을 알고 있습니다. 프로젝트 모델 라인을 따라 뭔가를 : has_many : comments, : through => : tickets – nick