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

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Masud Hossain asked in General

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| %>
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.