Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:15:58+00:00 2026-06-01T03:15:58+00:00

I have a 2 level nesting objects that i need help with My routes

  • 0

I have a 2 level nesting objects that i need help with

My routes look like this to normalise the url abit. instead of having a url that looks like this /projects/1/tasks/3/comments/3.

resources :projects do
  resources :tasks
end

resources :tasks do
  resources :comments
end

Model has the ‘has_many’, belongs_to methods.

I can create comments under each task and display them under the tasks, but on the ‘show’ template of the comments i would like to display a link back to the tasks, which i get an error because the tasks controller is asking for a project_id?

How would this normally done when dealing with 2 level nesting?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:15:59+00:00Added an answer on June 1, 2026 at 3:15 am

    I would do

    resources :projects, :shallow => true do
      resources :tasks do
        resources :comments
      end
    end
    

    Which is basically what you’re doing except you can’t generate a projects_task member path(ie projects/1/tasks/1) anymore they’d all just be task member paths(ie ‘/tasks/1’).
    Member paths include show, update, delete, edit

    But the project_tasks collection paths(ie projects/1/tasks) would still be available.
    Collection paths include index, create, new

    comment paths wouldn’t change. All comment paths would still include the task/:task_id prefix.

    Checkout the resources documentation for more info on that (also more info on member and collection also on that page.)

    But to actually solve your problem

    You need to look up the project_id when you link back to the project_tasks index. So you would need to do

    <%= link_to "Project Tasks Index", project_tasks_path(@task.project) %>
    

    That way the Task#index knows where the parent project is. This is the solution for both implementations.

    Check out the UrlFor documentation for more info on that.

    If you want access to a @project variable in a Comment view

    Then you just need to look up the project in the controller instead of at a view level. So basically

    class CommentsController < ApplicationController
      def show
        @task = Task.find(params[:task_id])
        @comment = @task.comments.find([:id])
    
        @project = @task.project
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class level price variable decalred inside a page, like this: public
I have an object containing nested objects and so on. The level of nesting
Is it possible to have microsecond level timestamps in log4j logging? This is on
I have a model like this class Parent < ActiveRecord::Base :has_many :kids end class
I got that the with statement help you to turn this: try: f =
I have the following problem: I have this multi-level array (nested array) which contains
I have a class that contains hierarchical data. I want to present this data
query level: beginner As part of a learning exercise I have written code that
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
The environment is Oracle 9 & 10. I do not have DBA level access.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.