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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:55:07+00:00 2026-05-18T01:55:07+00:00

I’ve been drying some code, one of this refactors is as following: I have

  • 0

I’ve been drying some code, one of this refactors is as following:
I have 3 controllers ( ConstructionCompanies, RealEstateCompanies, People) all of which had the following pattern:


class ConstructionCompaniesController < ApplicationController
before_filter :correct_user, :only => [:edit, :update]

private
   def correct_user
      @company = ConstructionCompany.find(params[:id]) 
      if(current_user.owner != @company.user)
        redirect_to(root_path)
      end
    end   

class RealEstateCompaniesController < ApplicationController
  before_filter :correct_user, :only => [:edit, :update]
...

private
   def correct_user
      @company = RealEstateCompany.find(params[:id]) 
      if(current_user.owner != @company.user)
        redirect_to(root_path)
      end
    end   

As you can see the correct_user is repeated in each controller.
So what I did inside I helper that is included for all of them I created a method:


def correct_user_for_seller_of_controller(controller)
    #"User".classify will return the class User etc.
    @seller = controller.controller_name.classify.constantize.find(params[:id])     
    redirect_to(root_path) unless (current_user == @seller.user)
  end                            

Know inside each controller I have:


class ConstructionCompaniesController < ApplicationController

  before_filter :only => [:edit, :update] do |controller| correct_user_for_seller_of_controller(controller) end           


class RealEstateCompaniesController < ApplicationController

 before_filter :only => [:edit, :update] do |controller| correct_user_for_seller_of_controller(controller) end           

I like the fact that is DRY now, but the problem is that it seems a little to complex for me, hard to understand. Did I went too far ?

  • 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-05-18T01:55:08+00:00Added an answer on May 18, 2026 at 1:55 am

    Add the correct_user method to the ApplicationController class.

    class ApplicationController
      def correct_user_for_seller_of_controller
        #"User".classify will return the class User etc.
        @seller = controller_name.classify.constantize.find(params[:id])     
        redirect_to(root_path) unless (current_user == @seller.user)
      end  
    end
    

    In your controllers use the new method as a filter method:

    class RealEstateCompaniesController < ApplicationController
    
     before_filter :correct_user_for_seller_of_controller, :only => [:edit, :update]
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.