-
Notifications
You must be signed in to change notification settings - Fork 2
Meta variables in controllers
Petr Marek edited this page Jan 14, 2019
·
1 revision
ApplicationControllerBase mixins includes a set_meta_variables method.
def set_meta_variables(instance, mappings = {})
m = {
title: :title,
image: :cover,
description: :perex,
meta_title: :meta_title,
meta_description: :meta_description,
}.merge(mappings)
...
endIt's used in PagesControllerBase mixin
set_meta_variables(@page)It tries to set view values for title and og: attributes from the passed instance given the mappings (defaults are shown above). Fails silently.
Both meta_title and meta_description have precedence over title and description, respectively.
Say we have a product with a name and description and no meta field, the target image is taken from a has_one :main_image.
# app/controllers/my_project/products_controller.rb
class MyProject::ProductsController < ApplicationController
def show
@product = MyProject::Product.find(params[:id])
set_meta_variables(@product, title: :name,
description: :description,
image: :main_image)
end
end