2010-06-05 117 views
1

不知道這是否已經回答過。自定義路由和聲明授權

有自定義路由給用戶。如果我直接訪問用戶/ users/5一切正常。如果我試着/ profile文件甚至/用戶/ CURRENT_USER與聲明授權我得到

map.profile "profile", :controller => "users", :action => "show" 
map.edit_profile 'profile/edit', :controller => 'users', :action => 'edit', :conditions => { :method => :get } 

我的ApplicationController中有

的before_filter { 「沒有一個ID找不到用戶」 | C | Authorization.current_user = c.current_user}

和我authorization_rules已經USER.ID也試過current_user.id。

role :user do 
    includes :guest 
    has_permission_on :users, :to => [:show, :edit ] do 
    if_attribute :id => is { user.id } 
    end 
end 

我在做什麼錯?

回答

3

對於自定義指數型路由使用
filter_access_to:所有

不是
filter_resource_access

了我太多。

1

我使用AuthLogic,但據我所知「current_user」不會通過路由訪問。

您需要檢查,在控制器中,如果PARAMS [:ID] == 「CURRENT_USER」(作爲一個字符串),然後以此爲基礎進行一些邏輯...即:

if params[:id] == "current_user" 
    @user_id = current_user.id 
else 
    @user_id = params[:id] 
end 
@user = User.find(@user_id) 

一個非常簡單的例子,但它應該說明你將需要從自定義路由中獲取current_user的邏輯類型。 您也可以將current_user的命名路由映射到它自己的控制器操作,但這不是非常RESTful,並且很可能會重複您已擁有的功能。

+0

滑了我的腦海,我想問: 是否有任何特別的原因會阻止你使用user_path(current_user.id)來生成鏈接?或者需要訪問current_user * need *的URL? – Robbie 2010-06-05 20:24:31

+0

實際上試圖不在路徑/ URL中使用current_user。對於一個用戶到他們的家鄉路徑它將/ profile和編輯將/ profile /編輯等現在prob是聲明授權需要訪問用戶id,我想從url路由。不太確定,這就是爲什麼我問。 – pcasa 2010-06-05 21:23:37