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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:34:13+00:00 2026-05-11T20:34:13+00:00

In a fit of unoriginality, I’m writing a blog application using Ruby on Rails.

  • 0

In a fit of unoriginality, I’m writing a blog application using Ruby on Rails. My PostsController contains some code that ensures that the logged in user can only edit or delete their own posts.

I tried factoring this code out into a private method with a single argument for the flash message to display, but when I did this and tested it by editing another author’s post, I got an ActionController::DoubleRenderError – “Can only render or redirect once per action”.

How can I keep these checks DRY? The obvious approach is to use a before filter but the destroy method needs to display a different flash.

Here’s the relevant controller code:

before_filter :find_post_by_slug!, :only => [:edit, :show]

def edit

  # FIXME Refactor this into a separate method
  if @post.user != current_user
    flash[:notice] = "You cannot edit another author’s posts."
    redirect_to root_path and return
  end
  ...
end

def update 
  @post = Post.find(params[:id])

  # FIXME Refactor this into a separate method
  if @post.user != current_user
    flash[:notice] = "You cannot edit another author’s posts."
    redirect_to root_path and return
  end
  ...
end

def destroy
  @post = Post.find_by_slug(params[:slug])

  # FIXME Refactor this into a separate method
  if @post.user != current_user
    flash[:notice] = "You cannot delete another author’s posts."
    redirect_to root_path and return
  end
  ...
end

private
def find_post_by_slug!
  slug = params[:slug]
  @post = Post.find_by_slug(slug) if slug
  raise ActiveRecord::RecordNotFound if @post.nil?
end
  • 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-11T20:34:14+00:00Added an answer on May 11, 2026 at 8:34 pm

    The before filter approach is still an ok option. You can gain access to which action was requested using the controller’s action_name method.

    before_filter :check_authorization
    
    ...
    
    protected
    
    def check_authorization
      @post = Post.find_by_slug(params[:slug])
      if @post.user != current_user
        flash[:notice] = (action_name == "destroy") ? 
          "You cannot delete another author’s posts." : 
          "You cannot edit another author’s posts."
        redirect_to root_path and return false
      end
    end
    

    Sorry for that ternary operator in the middle there. 🙂 Naturally you can do whatever logic you like.

    You can also use a method if you like, and avoid the double render by explicitly returning if it fails. The key here is to return so that you don’t double render.

    def destroy
      @post = Post.find_by_slug(params[:slug])
      return unless authorized_to('delete')
      ...
    end
    
    protected
    
    def authorized_to(mess_with)
      if @post.user != current_user
        flash[:notice] = "You cannot #{mess_with} another author’s posts."
        redirect_to root_path and return false
      end
      return true
    end
    

    You could simplify it more (in my opinion) by splitting out the different parts of behavior (authorization, handling bad authorization) like this:

    def destroy
      @post = Post.find_by_slug(params[:slug])
      punt("You cannot mess with another author's post") and return unless author_of(@post)
      ...
    end
    
    protected
    
    def author_of(post)
      post.user == current_user
    end
    
    def punt(message)
      flash[:notice] = message
      redirect_to root_path
    end
    

    Personally, I prefer to offload all of this routine work to a plugin. My personal favorite authorization plugin is Authorization. I’ve used it with great success for the last several years.

    That would refactor your controller to use variations on:

    permit "author of :post"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want do fit some sort of multi-variate time series model using R. Here
I am trying to fit TextViews in the screen, so that every textview contains
My application does not fit into the general purpose RDBMS schema category, I do
I am trying to fit a distribution to some data I've collected from microscopy
I'm trying to fit 3 pictures in one HTML page, such that maximum screen
I am trying to fit a sentence that changes often, in to a few
I need to fit some points from different datasets with straight lines. From every
I'm trying to fit a function consisting of several gauss bells to some experimental
I am try to fit an eGARCH model on an expanding basis using the
i need to fit the height of aside to that of body without regard

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.