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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:11:20+00:00 2026-06-09T21:11:20+00:00

In my application, I have an user model/controller. An user can have multiple videos,

  • 0

In my application, I have an user model/controller. An user can have multiple videos, images and blog items. User, and the items can have comments. So I’ve got the following controllers

  • user
  • user/comments
  • user/picture
  • user/picture/comments
  • user/video
  • user/video/comments
  • user/blog
  • user/blog/comments

The problem is, all the comments controllers are almost identical, and the code is becoming hard to manage. Now I’d like to specify a central place, e.g. a app-level CommentsController, which would have the methods to be called from sub-controllers.

What is the best way to do that?

How would for example the following code look after such a change:

class User::Picture::CommentsController < ApplicationController
  def delete_all
    @user = User.find(params[:user_id])
    @picture = @user.pictures.find(params[:picture_id])

    if @picture.has_access(current_user)
      @picture.comments.destroy_all

      redirect_to :back, :notice=>t(:actionsuccesful)
    else
      redirect_to :back, :alert=>t(:accessdenied)
    end
  end
end

The @user && @picture initializations are same among different methods (destroy, delete_all, create, index). Could they be moved into a before_filter which would be a sub-controller specific? And then, delete_all would be implemented in the CommentsController?

  • 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-09T21:11:21+00:00Added an answer on June 9, 2026 at 9:11 pm

    If the code is that generic, two options:

    1) a module including shared methods

    Example:

    module CommentsActions
      # actions, methods
    end
    
    class User::Picture::CommentsController <ApplicationController
      include CommentsActions
      #your additional actions
    end
    

    2) subclassing comment controllers from one controller

    Example:

    class CommentsController < ApplicationController
      # actions, methods, filters etc...
    end
    
    class User::Picture::CommentsController < CommentsController
      #your additional actions
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my application, I have a User model. Each user can have multiple (email)
I'm doing an online judge application, so I have a User model, a Problem
I have a model function that updates a user in my CodeIgniter application: //
My application will have a per machine (not per user) Startup shortcut. I can
My Rails application have a User model and a Group model, where User belongs
I'm working on a very basic practice application where a user can create multiple
I want to have one model & view that is served by multiple controllers
I have a basic application with a user model. When the user creates an
I would like to make my own user-mode NAT application. I have the folowing
I have a rails application with User and Machine data models, as well as

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.