2011-11-29 123 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