Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: 3 models and routing error
    primarykey
    data
    text
    <p>I have projects, which have tickets, which then have comments. I believe the code for the model associations are correct?</p> <pre><code>class Comment &lt; ActiveRecord::Base belongs_to :ticket, :dependent =&gt; :destroy belongs_to :project end class Ticket &lt; ActiveRecord::Base belongs_to :project has_many :comments, :dependent =&gt; :destroy end class Project &lt; ActiveRecord::Base has_many :tickets has_many :comments, :through =&gt; :tickets end </code></pre> <p>Before I click the button, the route is:</p> <pre><code>http://localhost:3000/projects/7/tickets/16 </code></pre> <p>When I click the submit button, the route changes to </p> <pre><code>http://localhost:3000/tickets/16/comments </code></pre> <p>How do I get it to read:</p> <pre><code>http://localhost:3000/projects/7/tickets/16/ </code></pre> <p>after submitting a new comment to show up on the <code>tickets/:id</code> view?</p> <p>When I submit and and create the comments on the ticket object, it gives an active record error:</p> <pre><code>ActiveRecord::RecordNotFound in CommentsController#create Couldn't find Project without an ID </code></pre> <p>using this controller:</p> <pre><code>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 </code></pre> <p>New to rails but it has to be with the model association or routing...</p> <p>My routing links...</p> <pre><code>Kaledoscope:ticket_taker Evolution$ rake routes project_tickets GET /projects/:project_id/tickets(.:format) {:action=&gt;"index", :controller=&gt;"tickets"} POST /projects/:project_id/tickets(.:format) {:action=&gt;"create", :controller=&gt;"tickets"} new_project_ticket GET /projects/:project_id/tickets/new(.:format) {:action=&gt;"new", :controller=&gt;"tickets"} edit_project_ticket GET /projects/:project_id/tickets/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"tickets"} project_ticket GET /projects/:project_id/tickets/:id(.:format) {:action=&gt;"show", :controller=&gt;"tickets"} PUT /projects/:project_id/tickets/:id(.:format) {:action=&gt;"update", :controller=&gt;"tickets"} DELETE /projects/:project_id/tickets/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"tickets"} projects GET /projects(.:format) {:action=&gt;"index", :controller=&gt;"projects"} POST /projects(.:format) {:action=&gt;"create", :controller=&gt;"projects"} new_project GET /projects/new(.:format) {:action=&gt;"new", :controller=&gt;"projects"} edit_project GET /projects/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"projects"} project GET /projects/:id(.:format) {:action=&gt;"show", :controller=&gt;"projects"} PUT /projects/:id(.:format) {:action=&gt;"update", :controller=&gt;"projects"} DELETE /projects/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"projects"} ticket_comments GET /tickets/:ticket_id/comments(.:format) {:action=&gt;"index", :controller=&gt;"comments"} POST /tickets/:ticket_id/comments(.:format) {:action=&gt;"create", :controller=&gt;"comments"} new_ticket_comment GET /tickets/:ticket_id/comments/new(.:format) {:action=&gt;"new", :controller=&gt;"comments"} edit_ticket_comment GET /tickets/:ticket_id/comments/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"comments"} ticket_comment GET /tickets/:ticket_id/comments/:id(.:format) {:action=&gt;"show", :controller=&gt;"comments"} PUT /tickets/:ticket_id/comments/:id(.:format) {:action=&gt;"update", :controller=&gt;"comments"} DELETE /tickets/:ticket_id/comments/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"comments"} tickets GET /tickets(.:format) {:action=&gt;"index", :controller=&gt;"tickets"} POST /tickets(.:format) {:action=&gt;"create", :controller=&gt;"tickets"} new_ticket GET /tickets/new(.:format) {:action=&gt;"new", :controller=&gt;"tickets"} edit_ticket GET /tickets/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"tickets"} ticket GET /tickets/:id(.:format) {:action=&gt;"show", :controller=&gt;"tickets"} PUT /tickets/:id(.:format) {:action=&gt;"update", :controller=&gt;"tickets"} DELETE /tickets/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"tickets"} root /(.:format) {:controller=&gt;"projects", :action=&gt;"index"} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload