Skip to content

Instantly share code, notes, and snippets.

@b1nary b1nary/_sidebar.html.erb
Last active Jan 19, 2017

Embed
What would you like to do?
ForwardMX.io Blog Post about creating a Blog in Rails
<a href="/blog/feed.rss" class="rss" target="_blank">
<i class="fa fa-rss" aria-hidden="true"></i> RSS Feed
</a>
<h3>Categories</h3>
<div class="list-group">
<a href="<%= blogs_path %>" class="list-group-item"><strong>Show All</strong></a>
<% Blog.pluck(:category).uniq.sort.each do |cat| %>
<a href="<%= blogs_path %>?category=<%= cat %>" class="list-group-item"><%= cat %></a>
<% end %>
</div>
class Blog < ApplicationRecord
has_attached_file :avatar, styles: { list: "900x400#", big: "2000x600#", thumb: "350x150#" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
def to_param
"#{self.id}-#{self.title.downcase.gsub(' ','-').gsub(/[^0-9A-Za-z-]/, '')}"
end
end
class BlogsController < ApplicationController
def index
@blogs = Blog.order(created_at: :desc)
@blogs = @blogs.where(category: params[:category]) if params[:category]
end
def show
@blog = Blog.find(params[:id])
end
def feed
@blog_articles = Blog.all
respond_to do |format|
format.rss { render :layout => false }
end
end
end
#encoding: UTF-8
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.channel do
xml.title "ForwardMX.io"
xml.author "ForwardMX"
xml.description "ForwardMX Tech & Bubble blog"
xml.link "https://forwardmx.io"
xml.language "en"
for article in @blog_articles
xml.item do
if article.title
xml.title article.title
else
xml.title ""
end
xml.author "ForwardMX.io"
xml.pubDate article.created_at.to_s(:rfc822)
xml.link blog_url(article)
xml.guid article.id
text = article.body
if article.avatar.exists?
image_url = article.avatar.url(:large)
image_caption = article.title
image_align = ""
image_tag = "
<p><img src='" + image_url + "' alt='" + image_caption + "' title='" + image_caption + "' align='" + image_align + "' /></p>
"
text = text += image_tag
end
xml.description "<p>" + text + "</p>"
end
end
end
end
<%
meta title: "FowardMX Blog",
description: "Tech und bubble about ForwardMX and the world",
twitter: {
title: "FowardMX Blog",
description: "Tech und bubble about ForwardMX and the world"
},
og: {
title: "FowardMX Blog",
description: "Tech und bubble about ForwardMX and the world"
}
%>
<br>
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<% @blogs.each do |blog| %>
<article class="blog">
<%= link_to blog_path(blog) do %>
<%= image_tag blog.avatar.url(:list), class: "img-responsive", alt: blog.title %>
<h2><%= blog.title %></h2>
<p><%= blog.description %></p>
<% end %>
</article>
<hr>
<% end %>
</div>
<div class="col-md-3 col-sm-4">
<%= render 'blogs/sidebar' %>
</div>
</div>
</div>
get '/blog/feed' => "blogs#feed", defaults: { format: 'rss' }
get '/blog/rss', to: redirect('/blog/feed.rss')
resources :blogs, only: [:index, :show], path: :blog
<%
meta title: @blog.title,
description: @blog.description,
keywords: @blog.keywords.split(",").map(&:strip),
twitter: {
card: 'summary_large_image',
site: '@forward_mx',
title: @blog.title,
description: @blog.description,
image: "https://forwardmx.io/#{@blog.avatar.url(:list)}",
creator: '@talkb1nary'
},
og: {
title: @blog.title,
type: 'website',
url: "#{request.original_url}",
image: "https://forwardmx.io/#{@blog.avatar.url(:list)}",
description: @blog.description
}
%>
<br>
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<article>
<%= image_tag @blog.avatar.url(:list), class: "img-responsive", alt: @blog.title %>
<h2><%= @blog.title %></h2>
<p class="lead"><%= @blog.description %></p>
<%= raw @blog.body %>
</article>
<hr>
<h3>Other articles</h3>
<div class="row">
<% Blog.where.not(id: @blog.id).order("RANDOM()").limit(3).each do |blog| %>
<div class="col-xs-4">
<div class="panel panel-default">
<div class="panel-body">
<a href="<%= blog_path(blog) %>" style="color:#222">
<%= image_tag blog.avatar.url(:thumb), class: "img-responsive", alt: @blog.title %>
<h4><%= blog.title %></h4>
</a>
</div>
</div>
</div>
<% end %>
</div>
</div>
<div class="col-md-3 col-sm-4">
<%= render 'blogs/sidebar' %>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.