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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:32:32+00:00 2026-05-27T02:32:32+00:00

I have several controllers that require a correct user for their edit/update/delete actions. What

  • 0

I have several controllers that require a correct user for their edit/update/delete actions. What is the Rails-way to accomplish the following:

Currently, in each controller I have the following code:

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

  # normal controller code

  private

  def correct_user
    @foo = Foo.find params[:id]
    redirect_to some_path unless current_user == @foo.user
  end
end

I have similar code in 3 controllers. I started to bring it out to a helper like this:

module ApplicationHelper
  def correct_user( object, path )
    if object.respond_to? :user
      redirect_to path unless object.user == current_user
  end
end

But I’m wondering if this is a good way to do it. What’s the accepted way to solve this?

Thank you

EDIT

The correct user check here is because I want to make sure it’s only the author who can make edits/deltes to each of the objects.

To clarify, the objects would be things like Questions and Posts. I don’t want to use something like CanCan as it’s overkill for something simple like this.

  • 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-27T02:32:32+00:00Added an answer on May 27, 2026 at 2:32 am

    I really like using RyanB’s CanCan, which allows you to both restrict access to actions based on the user, and centralize such authorization into basically a single file.

    • CanCan on GitHub: https://github.com/ryanb/cancan
    • Screencast explaining how to setup/use it: http://railscasts.com/episodes/192-authorization-with-cancan

    EDIT

    No problem. I hear you on CanCan – it takes a little while to get up and running on it, but it’s designed to do exactly what you’re asking – per object authorization.

    Alternative:
    Another way to do this is move your authoriship/current_user check to the ApplicationController class, from which all of your other Controllers inherit (so they will get that code through inheritance – and you don’t need to write the same code in multiple Controllers), and it would look something like…

    class ApplicationController < ActionController::Base
      ...
    
      helper_method :correct_user
    
      private
        def correct_user( object, path )
          redirect_to path unless object.user == current_user
        end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several controllers that take an instance of different classes each (Email, Call,
I'm running Rails 3, and I've got several controllers that all do something like
I have a Rails application with several models-views-controllers which have some similar characteristics, for
I have several controllers in a spring mvc application. They are regular beans that
I have a controller decorated with an AuthorizeAttribute. The controller contains several actions that
I have a view controller class that has to implement several protocols. Too keep
i have several controllers which will all be using common functionality. So i have
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.
Here is the thing. I currently have a tabBar controller, with several navigation controllers
Several of my controller actions have a standard set of failure-handling behavior. In general,

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.