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 9273015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:00:12+00:00 2026-06-18T16:00:12+00:00

In Rails, when I need: /comments and /posts/1/comments How do I best organize CommentsController?

  • 0

In Rails, when I need:

/comments

and

/posts/1/comments

How do I best organize CommentsController? E.g. let the routes share the index actions, or work with 2 controllers?

  • 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-18T16:00:13+00:00Added an answer on June 18, 2026 at 4:00 pm

    You can work with only one controller.

    I would go with a before_filter to check if the post_id param is present:

    class CommentsController < ApplicationController
      before_filter :find_post, only: [:index]
    
      def index
        if @post.present?
          ## Some stuff
        else
          ## Other stuff
        end
      end
    
      private
    
        def find_post
          @post = Post.find(params[:post_id]) unless params[:post_id].nil?
        end
    end
    

    And have in your routes (with the constraints of your choice) :

    resources :posts do
      resources :comments
    end
    resources :comments
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a nested comments system in a Rails 3 application that
In config/routes.rb: resources :posts do resources :comments end resources :pictures do resources :comments end
[Rails] I need to run some code only 40% of the times, how may
Rails: I need to set a condition in a has_many that needs the self.id
My end goal is local development of a Radiant CMS installation. So, need rails
I working in Rails and I need to call to an PHP-script. I can
I am using Ruby on Rails and I need to store a search result
I need to install Rails 2.3.2 and Rails 2.3.4 simultaneously on my Ubuntu 9.04
I need some references for creating a Rails app where each client/company has his
I need to install ckeditor in a rails project. I need a page with

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.