我有一個自定義路線設置中,將URL中的LOCATION_ID(見下文)Rails的康康舞 - 如何限制用戶訪問自定義URL路由
resources :menu_items, :path => "/location_menu/:location_id"
所以,當我打/location_menu/1
它會告訴我LOCATION_1菜單,/location_menu/2
將顯示location_2的菜單等
每個用戶關聯到多個位置(has_many :locations
)
我試圖用康康舞從查看某些MENU_ITEM URLS限制用戶。
例如:用戶1與位置1相關聯和2因此,他們只能查看頁面/location_menu/1
和/location_menu/2
。但他們無法查看/location_menu/3
。
我在控制器創建自定義方式作爲的before_filter:
before_filter :location_check
...
def location_check
@location = Location.find(params[:location_id])
authorize! :see_location, @location
end
在我ability.rb
can :see_location, MenuItem do |location| location && user.location_ids.include?(location.id) end
出於某種原因,這並不爲我工作。我可能做錯了什麼?如果你們能幫助我,我會非常感激!
謝謝。
做'user'和'location'是之間的關聯'has_many_through' – Viren 2013-08-01 09:57:23