Reputation: 19
I made a Ruby helper:
module FeedHelper
def nav_menu
image_tag("/assets/logo.jpg", alt: "Logo", class: "nav-image")
"<ul>".html_safe
link_to('Home', root_path, class: 'nav-link')
link_to('Lists', root_path, class: 'nav-link')
link_to('Profile', root_path, class: 'nav-link')
link_to('Admin page', root_path, class: 'nav-link')
if user_signed_in?
link_to('Logout', destroy_user_session_path, method: :delete, class: 'nav-link')
else
link_to('Login', new_user_session_path, class: 'nav-link')
end
"</ul>".html_safe
end
end
But for some reason, when I call this, it only shows one login or logout depending on whether I am already logged in.
Upvotes: 1
Views: 147
Reputation: 34308
All helpers are simple Ruby methods that return strings. In your case you call a bunch of helpers in your helper. These helpers all return strings, but you're not saving the output anywhere. The last string of your method is the only one that is returned, and that's the last </ul>
.
In order to make it work, you need to collect the outputs of all the helpers, and then return everything you collected at the end:
def nav_menu
out = image_tag("/assets/logo.jpg", alt: "Logo", class: "nav-image")
out << "<ul>".html_safe
out << link_to('Home', root_path, class: 'nav-link')
...
out << "</ul>".html_safe
out
end
However note that it may be a better solution to put your nav code into a partial, and/or into your application layout instead. See the answers here for some examples:
How can I make my navbar appear on every page in my rails app?
Upvotes: 1