undefined local variable or method `meta_title'

224 Views Asked by At

Trying to set up some meta_title tags for a site and I seem to have run into an error. Having checked through my files a few times I am not seeing the error. Any help in spotting it would be appreciated

I thought it would have been a spelling error somewhere but I am not seeing it. I also thought it might be because I needed to restart my local server since I messed with the config files but that also has not resolved my error

config/meta.yml

meta_product_name: "A PRODUCT NAME I HAVE REMOVED"
meta_title: "A PRODUCT DESCRIPTION OF SIMILAR LENGTH TO THIS"
meta_description: "A description of fair length to describe the above"
meta_image: "image.png" 
twitter_account: "@someone"

config/initializers/default_meta.rb

DEFAULT_META = YAML.load_file(Rails.root.join("config/meta.yml"))

app/helpers/meta_tags_helper.rb

module MetaTagsHelper
  def meta_title
    content_for?(:meta_title) ? content_for(:meta_title) : DEFAULT_META["meta_title"]
  end

  def meta_description
    content_for?(:meta_description) ? content_for(:meta_description) : DEFAULT_META["meta_description"]
  end

  def meta_image
    meta_image = (content_for?(:meta_image) ? content_for(:meta_image) : DEFAULT_META["meta_image"])
    # little twist to make it work equally with an asset or a url
    meta_image.starts_with?("http") ? meta_image : image_url(meta_image)
  end
end

app/controllers/application_controller.rb

  def default_url_options
    { host: ENV["DOMAIN"] || "localhost:3000" }
  end

app/views/layouts/application.html.erb

<title><%= meta_title %></title>

Given the above, I expect the page to load and that when I attempt to share it on FB, etc. for the image and blurb to be of my own design. All I am getting on the localhost is an error for an undefined meta_title

2

There are 2 best solutions below

0
On

I am not sure how this was an issue preventing the page load but this is the only thing I have touched since posting this question:

The meta_tags_helper.rb file may have had an extra space after the .rb on the name. I opened the rename tab, deleted a space and saved. Now the program works. I can't fathom why such a minor change would actually matter here.

0
On

My knowledge of helpers goes up to rails 3 or 4.

I did have to include the helper in a controller, in the scope I desired it to have impact on, like this:

# for instance, in application_controller.rb
include MetaTagsHelper

It shouldn't be different now, I hope.