2010-12-03 51 views
2

我已經在我的控制器下面的附件的Rails 3 - 慘慘 - 定義一個權限創建

def upload 

    @attachment = Attachment.build(:swf_uploaded_data => params[:attachment][:attachment], :user_id => current_user.id, :project_id => params[:space_id]) 
.... 
    end 

我從康康舞喜歡的是隻允許用戶上傳他們屬於一個PROJECT_ID。我證實了該控制器是獲得正確的信息,沒有尼爾斯

這是我的康康舞:這裏

can :upload, Attachment do |attachment| 
    Rails.logger.info 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX- include CanCan::Ability - ATTACHMENT' 
    Rails.logger.info attachment.inspect 
    Rails.logger.info attachment.project 

    current_user.try(:role, attachment.space) 
end 

問題,就是附件。是零,並且attachment.project是零?您如何解決CanCan的這個問題,以確保只有項目團隊成員可以將附件上傳到項目中?

謝謝

回答

5

我認爲最好的辦法是在與authorize!方法控制器動作一個較低的水平做到這一點。

所以......

#AttachmentController 

#Will remove it from cancan 
load_and_authorize_resource :except => [:upload] 

def upload 
@attachment = Attachment.build(:swf_uploaded_data => params[:attachment][:attachment], :user_id => current_user.id, :project_id => params[:space_id]) 
    #add the authorize logic explicitly here when you have the attachment model populated 
    authorize! :upload, @attachment 
end 

讓我知道這是否爲你的作品。

0

例如,如果你想允許創建僅用於電流回路事件:

您可以在視圖

link.... if can? :create, @loop.events.new 

使用,然後在控制器

skip_authorize_resource only: [:new, :create] 

... 

def new 
    @event.loop_id = @loop.id 
    authorize! :create, @event 
end 

#similar for create action