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 trialJonathan Fernandes
Courses Plus Student 22,784 PointsRails classes with helper
I need some help adding bootstraps "active" class to my links in my application controller. I can't figure out why it isn't working or I don't know a simpler way of getting this done.
Here is the embedded html:
<ul class="nav navbar-nav">
<li class="nav-item">
<%= link_to "Home", '#', :class => active_page(:webpageTitle, "Home") %>
</li>
<li class="nav-item">
<%= link_to "About", '#', :class => active_page(:webpageTitle, "About") %>
</li>
</ul>
here is my helper function in the application_helper.rb file:
# code for active page class
def active_page(title, current_link)
if title == current_link
return "nav-link active"
else
return "nav-link"
end
end
And each of my pages have this provide function to supply one of the arguments:
<% provide(:webpageTitle, "Home") %>
<h1>Hello Treehouse!</h1>
Any thought on why I keep getting the output of just "nav-link" as a class when I am on the active page?
2 Answers
Tim Knight
28,888 PointsJonathan,
Symbols are immutable, meaning their value remains constant during the entirety of the program. So you really shouldn't be setting temporary attributes, like a page title in my opinion.
See if setting your title as an instance variable (@title
) impacts how your method works.
Jonathan Fernandes
Courses Plus Student 22,784 PointsOkay, so I figured it out and I feel dumb as heck. I didn't create an instance of the webpageTitle variable on the application controller. Once I did, I was golden. So in the application controller I put this in under the meta tags:
<% current_page = yield(:webpageTitle) %>
and did this for my links:
<%= link_to "Home", root_path, :class => active_page(current_page, "Home") %>
Works like a charm!
Thanks for the help!
Tim Knight
28,888 PointsExcellent! Glad to hear you got it working.
Jonathan Fernandes
Courses Plus Student 22,784 PointsJonathan Fernandes
Courses Plus Student 22,784 PointsI did and it not only didn't work, it made my other helper stop working too, so I changed it back.
My other helper is to display the title with a provide symbol:
which I yield in my application controller like so:
<title><%= full_webpage_title(yield(:webpageTitle)) %></title>
So yeah, no luck with that... :(
Tim Knight
28,888 PointsTim Knight
28,888 PointsTry outputting the value of :webpageTitle on the page just to see what it's storing the value as for comparison.