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

  • Home
  • SEARCH
  • 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 7750991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:26:03+00:00 2026-06-01T11:26:03+00:00

Rails beginner here.. I have a users resource where I implemented a callback that’s

  • 0

Rails beginner here..

I have a users resource where I implemented a callback that’s supposed to prevent an admin user from deleting herself.

before_filter :admin_no_delete,    only: :destroy

def admin_no_delete 
  admin_id = current_user.id if current_user.admin?
  redirect_to root_path if params[:id] == admin_id 
end      

If this looks familiar to some, it’s from Michael Hartl’s rails tutorial, exercise #10 here but I tried to do it differently, not as he suggested.

My (lame) test for this fails

    describe "deleting herself should not be permitted" do
      before do
        delete user_path(admin) 
      end
      it { should_not redirect_to(users_path) }
    end

But exposing a delete link for the admin user just to test and clicking on that link, it seems like the callback actually succeeds in executing (redirecting to root_path).

I was able to invoke the destroy action using jQuery to delete the record being protected by the callback (using Web Inspector’s javascript console):

$.ajax({url: 'http://localhost:3000/users/104', type: 'DELETE', success: function(result){alert(result)} })

Looking for ideas on how to prevent a DELETE HTTP request from succeeding in this situation.. also any ideas on how to properly test for this kind of situation?

Thanks.

  • 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-01T11:26:04+00:00Added an answer on June 1, 2026 at 11:26 am

    Simple: params[:id] is a string, while admin_id is a Fixnum. You can just change it as follows and it should work:

    redirect_to root_path if params[:id].to_i == admin_id
    

    The logic you’re using seems a little odd to me, though. Why use a before filter if it’s just for one action, and why change the redirect? I think the logic should be directly in the destroy action and look something like this:

    def destroy
      unless current_user.admin? && current_user.id == params[:id].to_i
        User.find(params[:id]).destroy
        flash[:success] = "User destroyed."
      end
      redirect_to users_path
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner in rails. I have view that i try to print inside
Complete beginner to Rails here: In Rails: I have a Model Post, which hasmany
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
This is a beginner's rails learning confusion. When I learn rails, from time to
I am very much a beginner to rails and I have a specific need
I am beginner in ruby + rails. I have problem to get values in
I am a beginner-verging-on-intermediate rails developer that is working hard to improve my skills.
Rails has a nice idiom that makes it easy for you to have a
I am a beginner in Rails and I read that the Rails enforces the
I have a rails question that I have been unable to find an answer

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.