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"}
I have projects, which have tickets, which then have comments. I believe the code for the model associations are correct?
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
Before I click the button, the route is:
http://localhost:3000/projects/7/tickets/16
When I click the submit button, the route changes to
http://localhost:3000/tickets/16/comments
How do I get it to read:
http://localhost:3000/projects/7/tickets/16/
after submitting a new comment to show up on the tickets/:id
view?
When I submit and and create the comments on the ticket object, it gives an active record error:
ActiveRecord::RecordNotFound in CommentsController#create
Couldn't find Project without an ID
using this controller:
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
New to rails but it has to be with the model association or routing...
My routing links...
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"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的控制器中你需要做一些轻微的改变。在您拥有
@ticket.comments.new
的地方,您应该拥有@ticket.comments.build(params[:comment])
。例如:Build 将构建一个新评论,并将
ticket_id
设置为父级的id
。如果您执行new
,它将无法正确设置ticket_id
,因此它将无法重定向到自@ticket
以来给定的路线code> 和@comment
不会关联。来自 Rails 文档:
collection.build(attributes = {})
返回一个或多个集合类型的新对象,这些对象已使用属性实例化并通过外键链接到该对象,但尚未保存。
In your controller you need to make a slight change. Where you have
@ticket.comments.new
, you should have@ticket.comments.build(params[:comment])
. For example:Build will build a new comment with the
ticket_id
set to theid
of the parent. If you do anew
, it won't properly set theticket_id
, so it won't be able to redirect to the route given since@ticket
and@comment
won't be associated.From the Rails documentation:
collection.build(attributes = {})
Returns one or more new objects of the collection type that have been instantiated with attributes and linked to this object through a foreign key, but have not yet been saved.