Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources A Route to a Delete Action Filters

In Rails, Why Are Methods Symbols?

When defining a private method, say set_page, we can write it as the following in our private section

def set_page @page = Page.find(params[:id]) end

However, when we are utilizing this private method as a before_action in our controller, we write it as the following:

before_action :set_page, except: [:index, :new, :create]

Here, both the set_page method, as well as all of some of our normal crud methods, are being referenced as a symbol - as far as I'm aware symbols are the key reference in a hash. Are our methods in the PagesController class hashes?

1 Answer

Tim Knight
Tim Knight
28,863 Points

Hi Alex—

Symbols are kind of an "internalized string" in Ruby which just really means it's a light weight version of a string and in the example given you're just passing those into the before _action method as an argument.

You might to check out the answer on http://stackoverflow.com/questions/26786542/why-do-callbacks-use-symbols-in-ruby-on-rails which provides some helpful insight into why Rails (and Ruby) use symbols.