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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:41:01+00:00 2026-05-22T12:41:01+00:00

I’m building a small web app that allows users to list their goals. I

  • 0

I’m building a small web app that allows users to list their goals. I want the users to only be able to edit their own content. I’ve already got an authenticate function as a before_filter which checks to make sure someone is signed in, but it doesn’t check if the user is the creator of the content. I tried creating a second before_filter called correct_user that has the code as follows:

def correct_user
  @user = User.find(params[:id])
  redirect_to(user_path(current_user)) unless current_user?(@user)
end 

In addition here is the server output from running a get request to edit my own content

Started GET "/goals/31/edit" for 127.0.0.1 at 2011-05-18 15:22:38 -0400
  Processing by GoalsController#edit as HTML
  Parameters: {"id"=>"31"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 31) LIMIT 1
Redirected to http://localhost:3000/users/101 Completed 302 Found in 49ms
Completed 302 Found in 49ms

For clarity, the user_id I’m using is 101, and the goal_id I’m trying to edit is 31. Could someone explain exactly what is going on?

Also, I’m aware you can navigate this problem by using a gem called CanCan (as a similar question was answered about this), but is there a way to do it without using a gem? It seems like my simple little function should work, but could someone explain why it doesn’t?

  • 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-22T12:41:02+00:00Added an answer on May 22, 2026 at 12:41 pm

    params is the hash of all parameters being sent (via url or form fields, etc) to your action. The name of the parameter, if present in the URL, is defined in your routes file. For your goals controller routes, you probably (presumably) have:

    goals_path: /goals/
    goal_path: /goals/:id
    edit_goal_path: /goals/:id/edit
    

    Because you are getting /goals/31/edit, params[:id] is 31, the id of the goal you are editing. The first line in correct_user is finding the User whose id matches the id in the params hash (goal_id). So really, you should be doing something like this:

    def correct_user
      user = Goal.find(params[:id]).user if params[:id]
      redirect_to user_path(current_user) unless current_user?(user)
    end
    

    This says, find the goal that somebody wants to edit (from params[:id]), and give me the user associated with it ( you didn’t post your goal model, I am assuming Goal belongs_to :user but you may have named it “creator” or “owner” instead). Redirect if the user is not the same as the current user logged in. Your previous code was attempting to find a User with the same id as the goal being edited.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.