Reputation: 889
Hi I have a rails application with one model that I want to show a loop of in the footer which appears on every page.
In the the Variety Model
class Variety < ApplicationRecord
belongs_to :request
end
I have found that adding:
@varieties = Variety.all
to every view works but i am sure this is bad practice.
Upvotes: 3
Views: 315
Reputation: 4115
for this case, it's better to do something like this, in your application controller, you add a before action, so on every action, independent of the controller you are looking for, it will fill the instance variable
class ApplicationController < ActionController::Base
before_action :fill_varieties
private
def fill_varieties
@varieties = Variety.all
end
end
and then after this, create a partial on views/layouts/_footer.html.erb
<ul>
<% @varieties.each do |variety| %>
<li><%= variety.name %></li>
<% end %>
</ul>
of course you need to change the view with the code about how you want to show it.
and on your views/layouts/application.html.erb just call the partial
<%= render "layouts/footer" %>
and it will render on every request
Upvotes: 3