SEO Meta tags plugin
Installation
Add to your project using your Bundler Gemfile:
gem 'seo_meta'
Include in your model by calling the method is_seo_meta
in the class declaration,
for example with Page
:
class Page < ActiveRecord::Base
is_seo_meta
end
Run the generator:
rails generate seo_meta
Migrate the database (unless you want to read 'Migrating existing data' below first):
rake db:migrate
Migrating existing data
This step is only if you already implemented SEO meta tag fields in your model.
At this point you could add to the migration that is generated in db/migrate/
logic to migrate across your existing data and remove the columns from your model
afterward, for example with Page
:
class CreateSeoMeta < ActiveRecord::Migration[4.2]
def self.up
existing_pages = ::Page.all.map(&:attributes)
::SeoMeta.attributes.each do |field|
if ::Page.column_names.map(&:to_sym).include?(field)
remove_column ::Page.table_name, field
end
end
::Page.reset_column_information
::Page.module_eval do
is_seo_meta
end
existing_pages.each do |page|
::Page.find(page['id']).update_attributes({
::SeoMeta.attributes.keys.inject({}) {|attributes, name|
attributes.merge(name => translation[name.to_s])
}
})
end
end
def self.down
existing_pages = ::Page.all.map(&:attributes)
::SeoMeta.attributes.each do |field, field_type|
unless ::Page.column_names.map(&:to_sym).include?(field)
add_column ::Page.table_name, field, field_type
end
end
::Page.reset_column_information
existing_pages.each do |page|
::Page.find(page['id']).update_attributes({
::SeoMeta.attributes.keys.inject({}) {|attributes, name|
attributes.merge(name => translation[name.to_s])
}
})
end
end
end
Now, run:
rake db:migrate
Form Partial
You can use the included partial if you want a ready made form for the new SEO fields.
Note that the :form
local variable is required and should be a form builder object
from a form_for
block, for example:
<%= form_for @page do |f| -%>
<%= render '/seo_meta/form', :form => f %>
<% end %>
Anything else?
Nope, all done!