Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - How can I detect if the content_for content was provided?

I want to detect if content was provided for content_for tag in my template, and if not fall back to default value:

<title>
  <% if content_is_provided -%>
    <%= yield :title -%>
  <% else -%>
    404 - Page Unknown
  <% end -%>
</title>

Any easy way to detect this? I tried <% if :title -%> but that didn't do much. thanks.

like image 904
sa125 Avatar asked Jan 23 '23 04:01

sa125


2 Answers

You can streamline the code further:

def content_exists?(name)
  return instance_variable_get("@content_for_#{name}")
end
like image 106
Toby Hede Avatar answered Jan 25 '23 23:01

Toby Hede


Try this:

<title>
  <%= yield :title || "404 - Page Unknown" -%>
</title>
like image 45
John Topley Avatar answered Jan 25 '23 23:01

John Topley