Note that there are some explanatory texts on larger screens.

plurals
  1. POLimit users to only access their profiles using Devise
    primarykey
    data
    text
    <p>I have currently set my app so that on successful sign in the app redirects the user to their profile at <code>localhost:3000/users/id</code> however if I am the first user <code>id =&gt; 1</code> and type <code>users/2</code> I have full access to this profile. I have been trying to find how to stop this using devise. I'm pretty new to rails so I'm sure I'm missing something simple, I have used the <code>before_filter :authenticate_user!</code> but this is obviously just checking if a user is signed in, but doesn't limit access to other users' profiles. I have read a bit on CanCan but this seems a bit overkill for what I am trying to achieve. Any pointers much appreciated. </p> <p>users_controller.rb</p> <pre><code>class UsersController &lt; ApplicationController before_filter :authenticate_user! before_filter :user_authorization def index @users = User.all end def show @user = User.find(current_user[:id]) end private def user_authorization redirect_to(root_url) unless current_user.id == params[:id] end end </code></pre> <p>This is being reported from the server:</p> <pre><code>Started GET "/users/2" for 127.0.0.1 at 2012-06-24 13:00:38 +0200 Processing by UsersController#show as HTML Parameters: {"id"=&gt;"2"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]] Redirected to http://localhost:3000/ Filter chain halted as :user_authorization rendered or redirected Completed 302 Found in 20ms (ActiveRecord: 0.8ms) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload