我有项目,其中有门票,然后有评论。我相信模型关联的代码是正确的?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"}
在您的项目中运行'rake routes'。你得到了什么? – cocoahero