Bohn
Bohn

Reputation: 26929

Methods in Controllers

I am looking at a sample Rails application and see some strange things. Well just strange to me because my past experience was with C#.

So in the ApplicationController I have a "private" method like this:

 private

    def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      session[:cart_id] = cart.id
      cart
    end

and then in orders_controller class I have another method that in its body it is saying something like:

def new
   @cart = current_curt
// ....
end

What happened ? It was private but we can access it? And we don't need to create an instance of it before accessing it ? Can someone talk a little bit about how the methods in controllers work together in Rails?

Upvotes: 0

Views: 41

Answers (1)

Dave Newton
Dave Newton

Reputation: 160321

There is an instance of the controller, instantiated by the framework, per-request.

And yes, subclasses can access the method (as running it would show).

Upvotes: 2

Related Questions