Last active
January 19, 2017 10:56
-
-
Save b1nary/8985cf21eb1076da0590926925fe1937 to your computer and use it in GitHub Desktop.
ForwardMX.io Blog Post about creating a Blog in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/blog/feed' => "blogs#feed", defaults: { format: 'rss' } | |
get '/blog/rss', to: redirect('/blog/feed.rss') | |
resources :blogs, only: [:index, :show], path: :blog |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
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