2011-11-29 136 views
0

我有項目,其中有門票,然後有評論。我相信模型關聯的代碼是正確的?Rails 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 

新Rails,但是它必須是與模型的關聯或者路由器

我的路由鏈接...

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

在您的項目中運行'rake routes'。你得到了什麼? – 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 

構建將建立與ticket_id設置爲父的id新評論。如果你做了new,它不會正確設置ticket_id,所以它將不能重定向到給定的路由,因爲@ticket@comment將不會關聯。

從Rails文檔:

collection.build(attributes = {}) 返回已實例化的屬性,並通過外鍵鏈接到該對象,但尚未保存的集合類型的一個或多個新的對象。

+0

是的,我已經看到了構建方法。那兩個模型的關聯? – nick

+0

是的,看我的更新。 –

+0

謝謝肖恩!我爲嵌套路由使用了多個級別,給我諸如project_ticket_comments_path之類的路由。試圖找到一種解決方案,不會進行這種類型的嵌套。我知道我需要檢查我的模型關聯,沿着項目模型的行:has_many:comments,:through =>:ticket – nick