Search code examples
ruby-on-rails-3cancancustom-action

rails3 cancan load_resource for custom actions


I am using cancan 1.6.8 in my application.

I have to create a Cart in my custom method. So I have added custom actions in my routes.rb. Example:

resources :carts
   collection do
     get "new_cart"
     post "create_cart"
   end
end

How to initialize new object(@cart = Cart.new) using load_resource for my custom methods(new_cart, create_cart) ?


Solution

  • Specify :new which actions are new resource actions in addition to :new and :create. Pass an action name into here if you would like to build a new resource instead of fetch one.

    Example:

    load_resource :new => [:new_cart, :create_cart]
    

    Reference http://rdoc.info/github/ryanb/cancan/master/CanCan/ControllerAdditions/ClassMethods:load_resource