Skip to main content

How do I make certain partials show everywhere (ie navbar)?

General • Asked by Masud Hossain

I'm trying to have my 'conversations' partial which is a list of all active conversations show up on the navbar, but of course I'm getting a no method error.

I figured you can copy the conversationscontroller's instance variables and add them to the applicationcontroller, but i think there's a bit more to it than that.

Is there a video on gorails.com for working with stuff like this?


I don't have a video on it, but that's a good topic. My Guides link in the navbar is actually like this.

What you want to do is setup a before_action to set your instance variables so you can use them in your partials that you include on every page. That will set it up so that you always have them available and can display them on every page.

I'll have to make a screencast on this as well!


before_action is definitely the way to go! Just treat it like any other method... I use this for a contact form on every page...

#controllers/application_controller.rb
before_action :set_contact

private
 def set_contact
    @contact = Contact.new
 end

then your partial is setup as you'd expect:

#views/shared/_contact_form.html.erb
<%= form_for @contact, url: user_contact_path do |form| %>

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.