Skip to content

Instantly share code, notes, and snippets.

@beneggett
Created January 13, 2015 23:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save beneggett/1b35d51aec6b3f0fa51f to your computer and use it in GitHub Desktop.
Save beneggett/1b35d51aec6b3f0fa51f to your computer and use it in GitHub Desktop.
SEO tags in rails
# admin/seo/_seo_form.html.erb
<div class='full-inputs'>
<%= simple_form_for seo, remote: true, url: admin_update_seo_path(seo) do |f| %>
<%= f.input :content, required: true, as: :text, input_html: {rows: 2, maxlength: "#{ seo.name == 'title' ? '70' : '155' }"}, label: seo.name.try(:humanize), hint: "<span class='character-count green'> </span> #{seo.name.try(:humanize)} should be no longer than #{ seo.name == 'title' ? '70' : '155' } characters" %>
<div class='text-center'>
<button class='btn btn-primary'>Update</button>
<a href="#<%= page %>" class='cancel-seo btn btn-warning' data-attribute="<%=f.object.id %>"> Cancel</a>
<% end %>
</div>
<script>
$('.cancel-seo').click(function() {
$("#edit_meta_tag_<%= seo.id %>").remove();
$("#tag-<%= seo.id%> i.icon-pencil").show();
});
$('#edit_meta_tag_<%= seo.id %> textarea').keyup(function(){
var max=$(this).attr('maxlength');
var valLen=$(this).val().length;
$('#edit_meta_tag_<%= seo.id %> .character-count').text( valLen+'/'+max)
});
</script>
# admin/seo/edit.js.erb
$("#tag-<%= @seo.id%> i.icon-pencil").hide();
$("#tag-<%= @seo.id%>").append("<%= j(render 'seo_form', seo: @seo, page: params[:page]) %>");
# admin/seo/index.html.erb
<div class='page-header'>
<h1> SEO Tags</h1>
</div>
<ul class='unstyled'>
<% site_map_pages.each do |page| %>
<% path = Rails.application.routes.recognize_path(send("#{page.first}_url")) %>
<% tags = MetaTag.find_all_by_method_and_controller(path[:action], path[:controller]) %>
<div id="<%= page.second.parameterize %>" class='well'>
<ul class='unstyled'>
<li><h3><%= link_to page.second, send("#{page.first}_path"), target: "_blank"%></a></h3></li>
<% tags.each do |tag| %>
<li id="tag-<%= tag.id %>"><%= link_to admin_edit_seo_path(tag, page: page.second.parameterize), remote: true, class: 'edit-seo', data: {attributes: tag.id} do %><i class='icon-pencil'></i><% end %><strong> <%= tag.tag_type.try(:humanize) %> <%= tag.name.try(:humanize) unless tag.name.downcase == 'title' %> : </strong> <span class='seo-content'><%= tag.content %></span>
<%end %>
</ul>
</div>
<% end %>
</ul>
# admin/seo/update.js.erb
$("#edit_meta_tag_<%=@seo.id %>").remove();
$("#tag-<%= @seo.id%> i.icon-pencil").show();
$("#tag-<%= @seo.id%>").append("<span class='green'> <i class='icon-ok'> </i> Updated Successfully</span>");
$("#tag-<%= @seo.id%> span.seo-content").html("<%=@seo.content.try(:html_safe) %>")
# admin/seo_controller.rb
class Admin::SeoController < Admin::AdminController
skip_before_filter :permit_seo!
def index
@metatags = MetaTag.all
end
def edit
@seo = MetaTag.find(params[:id])
respond_to do |format|
format.html
format.xml
format.js
end
end
def update
@seo = MetaTag.find(params[:id])
if @seo.update_attributes(params[:meta_tag])
respond_to do |format|
format.html
format.xml
format.js
end
else
@errors = true
render :edit
end
end
end
# If you want to restrict access to admin's with seo role to only hit seo controller
before_filter :permit_seo!
private
def permit_seo!
if current_user.has_role? :seo
redirect_to admin_seo_path
end
end
<% unless @meta_data.blank? %>
<% @meta_data.each do |md| %>
<% if md.tag_type == "meta" %>
<meta name ="<%= md.name %>" content="<%= md.content %>" />
<% else %>
<%= content_tag md.tag_type, md.content %>
<% end %>
<% end %>
<% else %>
<title><%= content_for?(:html_title) ? yield(:html_title) : 'My default title' %> - My site name.</title>
<meta name="description" content="<%= (html_description = yield :html_description) ? html_description : 'My site description' %>">
<% end %>
...
before_filter :get_meta_tags
def get_meta_tags
@meta_data = MetaTag.find_all_by_method_and_controller(request.path_parameters[:action], request.path_parameters[:controller])
end
...
create_table "meta_tags", :force => true do |t|
t.string "tag_type"
t.string "name"
t.string "method"
t.string "content"
t.string "controller"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "article_id"
end
class MetaTag < ActiveRecord::Base
attr_accessible :controller, :method, :tag_type, :name, :content, :article_id
end
namespace :admin do
get '/seo', to: 'seo#index', as: :seo
get '/seo/:id', to: 'seo#edit', as: :edit_seo
put '/seo/:id/update', to: 'seo#update', as: :update_seo
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment