繁体   English   中英

自定义路由和声明授权

[英]Custom Routes and Declarative Authorization

不知道以前是否已经回答过这个问题。

有自定义路由到用户。 如果我直接访问用户/ 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

我究竟做错了什么?

对于自定义索引类型路由使用
filter_access_to:全部

而不是
filter_resource_access

也帮我了

我使用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,并且[很可能]会复制您已有的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM