w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Rails Tutorial: Duplicate method names, remember and remember(user), forget and forget(user)

Ruby doesn't support method overriding, if several methods with the same name are defined in a class - the latest overwrites previous definitions.

In this case, the methods that are in user.rb can only be accessed/invoked through a user object.

def remember(user)
    user.remember // invoked by user object
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
end

You will find that if you send a non-user object to your helper method, it will return an error as this method can only be accessed by the user object.

This works the same if you were to define methods with the same name in separate classes as they can only be accessed through the respective classes.

If you were to include your helper in a controller and add a method in your controller with the same name as the helper method, your controller method would override the former method. Which would be the same as adding two methods with the same name in the same controller. As stated above, ruby doesn't allow method overriding.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.