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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:20:19+00:00 2026-06-05T20:20:19+00:00

Dumb question time: In the following request spec, I try to make sure that

  • 0

Dumb question time: In the following request spec, I try to make sure that the first user in my db can’t be edited (by anyone besides the first user).

# user is not logged in during these tests

# variant 1 - this passes
describe "first user" do
  let(:first_user){ FactoryGirl.create(:admin) }

  # use put to modify the user
  before { put user_path(first_user, email: 'tst@test.com') }

  # this passes, the response is a redirect
  specify { response.should redirect_to(root_path) }
end

# variant 2 - this test fails
describe "first user" do
  let(:first_user){ FactoryGirl.create(:admin) }

  # this fails, email is updated
  it "can't be updated or edited" do
    expect do
      first_user.update_attributes(email: 'email@test.com')
    end.not_to change(first_user.reload, :email)
  end
end

The two tests appear to do the same thing, but one fails and one passes. I guess my understanding sucks here. Should update_attributes, as called in the failing test, invoke my controller’s before filter:

# users_controller.rb
before_filter correct_user, only: [:edit, :update]

private

# pretty messy, but ensures that ordinary users can only
# edit their own accounts, that admin users can
# edit all accounts, except for the first one. 
# I believe it also ensures that the first_user 
# can only be edited by the owner of the first account, i.e. me
# due to the fact that the first condition of the `unless` clause will pass
# if the current_user is the first_user. The complexity is necessary to prevent
# other admins, from being able to edit the first_user.
def correct_user
  @user=User.find(params[:id])
  redirect_to(root_path, only_path: true) unless current_user?(@user) || ( current_user.admin? && !first_user?(@user) )
end

def first_user?(user)
  user==User.first
end

Does update_attributes ignore my before_filter? Why doesn’t put?

  • 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-05T20:20:21+00:00Added an answer on June 5, 2026 at 8:20 pm

    update_attributes isn’t a request, it’s a model method–filters are meaningless outside a request context.

    “put” is a request, so filters are run.

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

Sidebar

Related Questions

I'm certain this is a dumb newbie question, but this is the first time
Not a full-time jQuery nor Javascript dev so sorry if dumb question. I think
Perhaps it is a dumb question but I am confused with the following: Are
Maybe its a very dumb question but I hope you can give me some
Here's a dumb question which I can't find an answer to: I have a
This may be a dumb question, and based on the fact that googling has
First of all sorry of asking such a dumb question, I am quite a
This might be a dumb question but I can't get this super simple script
I am not sure whether I am asking a dumb question again or not
This may be a dumb question, I'm 99% sure it does, but I need

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.