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