Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#routes.rb
get "blog/category/:category/" => "posts#by_category", as: :post_category
get "blog/category/:category/index.html" => "posts#by_category"
get "blog/page-:page/" => "posts#index", as: :blog
get "blog/page-:page/index.html" => "posts#index"
get "blog/:slug/" => "posts#by_slug", as: :post_slug
get "blog/:slug/index.html" => "posts#by_slug"
get "blog" => "posts#index", as: :blog
get "blog/index.html" => "posts#index"
get "rss.xml" => "posts#rss", format: :rss, as: :posts_rss
#posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC")
unless params[:admin]
@posts = @posts.where(visible: true)
end
@posts = @posts.page(params[:page])
end
def rss
@posts = Post.where(visible: true).order("created_at DESC").limit(20)
respond_to do |format|
format.rss { render layout: false }
end
end
def by_slug
@post = Post.where(slug: params[:slug]).first
render action: :show
end
def by_category
@posts = Post.order("created_at DESC").where("lower(keywords) = ?", params[:category].gsub(/\-/, ' '))
unless params[:admin]
@posts = @posts.where(visible: true)
end
@posts = @posts.page(params[:page])
render action: :index
end
end
#views/posts/index.html.erb
<% title "Blog of Tomas Varaneckas" %>
<div class="row blog">
<%= render partial: "shared/nav" %>
</div>
<div class="row blog">
<div class="span8">
<% for post in @posts %>
<div class="well post">
<%= render :partial => 'post', locals: { post: post } %>
<div class="comments-link">
<%= link_to "0 Comments", "#{post_slug_path(post.slug)}#disqus_thread" %>
</div>
</div>
<% end %>
<%= paginate @posts %>
</div>
<div class="span4">
<%= render :partial => 'categories' %>
</div>
</div>
</table>
<% if params[:admin] %>
<p><%= link_to "New Post", new_post_path %></p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment