if statement - Rails - Trouble with setting var and getting value on layout -
i'm having newb moment, trying find right (rails) way set variable on show.html.erb , value on layout.
in show.html.erb setting var so:
<% @meta_title = @content.meta_title %>
in layout, want set <title>
if defined , has length, otherwise want use default site setting (loaded config.yml).
in /views/layouts/public.html.erb
<%= @meta_title ? @meta_title : app_config[:site][:title] %>
i've tried many variations can't seem nail conditions down, things this:
<%= !@meta_title.blank? ? @meta_title : app_config[:site][:title] %> <%= !@meta_title.nil? || meta_title.length? ? @meta_title : app_config[:site][:title] %>
this should easier me have difficulty w/ if, unless
statements. can lend hand? thanks!
layouts cannot variables show templates , there no reason on show action since want in layout.
in application_helper.rb:
def meta_title(title) if title title else title_constant #or add text here such "my home page" end end
then in layout:
<%= meta_title(@meta_title) %>
this give default of instance variable if there one. don't forget set constant in config/initializers/constants.rb
or use text in helper.
Comments
Post a Comment