他們的個人資料我已經當前設定的我的應用程序,以便在應用中的成功的標誌在localhost:3000/users/id
用戶他們的個人資料重定向但是如果我是第一個用戶id => 1
並鍵入users/2
我已經完全進入此配置文件。我一直在試圖找到如何使用設計來阻止這種情況。我很新的軌道,所以我敢肯定我錯過了一些簡單的東西,我已經使用了before_filter :authenticate_user!
,但這顯然只是檢查用戶是否登錄,但不限制訪問其他用戶的配置文件。我看了一下CanCan,但這似乎有點矯枉過正,我試圖實現。任何指針非常讚賞。限制用戶只能訪問使用設計
users_controller.rb
class UsersController < 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
這正在從服務器報告:
Started GET "https://stackoverflow.com/users/2" for 127.0.0.1 at 2012-06-24 13:00:38 +0200
Processing by UsersController#show as HTML
Parameters: {"id"=>"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)
使用current_user方法。請參閱文檔 – vlain