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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:35:38+00:00 2026-05-26T09:35:38+00:00

I run a before_filter on some actions to check if the user is the

  • 0

I run a before_filter on some actions to check if the user is the current_user.

before_filter :correct_user,  :only => [:edit, :update, :destroy]

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    redirect_to current_user, :notice => "User updated!"
  else
    redirect_to current_user, :notice => "User not updated. waa waa."
  end
end

private
def correct_user
  if current_user != @user        
    redirect_to root_url, :notice => "Cannot act on different user."
  end
end

Not sure if this is the best way to do things, but it works (maybe it is better practice to simply use current_user instead of finding @user via params?)

Now User has_many photos, and in my photos index view, I list all the user’s photos and allow for the user to set any one photo as a profile photo. The user table has a column called primary_photo_id, to hold this ID, and I use a link_to to set this:

=link_to "Make this your profile photo", user_path(@user, :user => {:primary_photo_id  => "#{photo.id}"}), :method => :put

The problem is that the before_filter kicks in and will prevent this from working because the @user that is retrieved via params[:id] fails because it is not the right params. If I remove the before_filter, it works fine but then it’s not checking for the correct user anymore.

(a second somewhat related question is why the above code works but this one:

=link_to "Make this your profile photo", user_path(@user, :primary_photo_id  => "#{photo.id}"), :method => :put

doesn’t.

Thanks. I’m pretty new to rails and programming so anything you can say regarding my specific question, and any bad practices I’m doing with the code here, is very much appreciated.

  • 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-26T09:35:38+00:00Added an answer on May 26, 2026 at 9:35 am

    The before filter (#correct_user) runs before #update, so your instance variable @user isn’t set yet when you are comparing it in the filter if you haven’t set it in another before filter first. Your execution sequence looks like:

    1. run #correct_user – compare current_user to @user (if unset, this is nil). these will only ever match if the user isn’t logged in I’m guessing
    2. assuming we made it through, run #update and look up @user

    Probably the easiest way to address your problem is just to move the @user lookup into the before filter:

    before_filter :correct_user,  :only => [:edit, :update, :destroy]
    
    def edit
      # .. as before, but no need to look up user first
    end
    
    def update
      if @user.update_attributes(params[:user])
        redirect_to current_user, :notice => "User updated!"
      else
        redirect_to current_user, :notice => "User not updated. waa waa."
      end
    end
    
    def destroy
      # .. as before, but no need to look up user first
    end
    
    private
    
    def correct_user
      @user = User.find(params[:id])
      if current_user != @user        
        redirect_to root_url, :notice => "Cannot act on different user."
      end
    end
    

    Since @user is now being located in the filter, there is no need to look it up again in each of your controller actions. Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you run some script before the uploading of a file starts in php?
I need to run some methods before the first call of a wcf service,
I'm currently making a webpage and I have run in to some problems with
I've run into some confusing behavior in Analysis Services 2005 (and 2008 R2) and
I have a check_user_access_control before_filter in my ApplicationController that checks the logged user's roles
I want to run n fold cross validation on some classifiers I am using.
Is there a way to run the changed files through a filter before doing
I want a validation to run before a record gets updated. I know of
How can I force the input's onchange script to run before the RangeValidator's script?
I want to have a Mercurial hook that will run before committing a transaction

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.