Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlex Bannon
4,841 PointsIn 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
28,888 PointsHi 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.