1

不知道这个问题之前有没有回答过。

为用户提供自定义路线。如果我直接访问用户 /users/5 一切正常。如果我尝试 /profile 甚至 /users/current_user 使用声明式授权,我会得到“找不到没有 ID 的用户”

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

我的 ApplicationController 有

before_filter { |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

我究竟做错了什么?

4

2 回答 2

3

对于自定义索引类型路由,请使用
filter_access_to :all

而不是
filter_resource_access

也得到了我。

于 2011-05-05T17:46:28.117 回答
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,并且 [很可能] 会重复您已经拥有的功能。

于 2010-06-05T20:22:17.140 回答