2014-09-04 44 views
0

在我的Products模型的show動作中,我需要檢查兩件事情:
1.與產品模型實例關聯的user_id與current_user的id相同
2.當前用戶的braintree_customer_id爲零。Rails before_filter檢查show action中的屬性

如果訪問產品的展示頁面的用戶既是產品的創建者,也有nil braintree_customer_id,我需要執行一些操作。我只是沒有真正與before_filters合作,也不知道如何寫這個。這裏是我的表演動作:

def show 
    @product = Product.find(params[:id]) 
    respond_to do |format| 
     format.html 
    end 
    end 

回答

1

在產品控制器添加此

before_filter :check_id, only: [:show] 

def check_id 
    @product = Product.find(params[:id]) 
    if @product.user == current_user and current_user.braintree_customer_id.blank? 
    # do something 
    else 
    # do something else 
    end 
end 
0
You can try something like this 

before_filter :check_ids, :only => [:show] 

def check_ids 
    @product = Product.find(params[:id]) 
    if @product.user_id == current_user.id and current_user.braintree_customer_id.blank? 
     return true 
     # this will allow you to load show method 
    else 
    # show some error message 
    return false 
    end 
end