У меня есть проекты, у которых есть тикеты, которые затем имеют комментарии. Я считаю, что код для ассоциаций моделей правильный?
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
Новичок в рельсах, но это должно быть связано с ассоциацией моделей или маршрутизацией ...
Мои маршрутные ссылки ...
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"}