Reputation: 14279
I have a yield
in my application.html.erb
,
<%= yield(:alert_area) %>
... and I want to set content for this area for every page on the site (that uses the layout). The content cannot be hardcoded in the view because it must only display in certain situations (i.e. "Your profile is incomplete, click here to complete it!")
How can I do this? I can know I can try using content_for
in the ApplicationController
but that seems like it is breaking the MVC pattern by putting content in the controller. Plus, I was hitting a dead end doing it that way anyway.
What is the proper Rails MVC way to stick dynamic content on every page of the site?
Upvotes: 1
Views: 70
Reputation: 27971
As well as what bukk530 suggests, instead of a yield
/content_for
construct use a render
/partial
construct, and in the partial just have a conditional so it only renders something when you want it to. Eg.
- flash.each do |name, message|
%div{ class: "flash flash-#{name}" }= message
Upvotes: 1
Reputation: 1895
The best way is using flash
, it is stored in the session and it shows only one time after it has been set.
Example:
application.html.erb:
<span class="error"><%= flash[:error] %></span>
controller:
def raise_error
flash[:error] = "Hi! i'm an error!" # Use i18n here
redirect_to :somewhere
end
Upvotes: 1