Sha256: d4a2da9e5ceeabab072aaafbb86f25b2e38e801ecdae75fea40117b725f9841b

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

class Admin::Blog::PostsController < Admin::Blog::BaseController
  
  before_action :load_blog
  before_action :build_post, :only => [:new, :create]
  before_action :load_post,  :only => [:edit, :update, :destroy]

  def index
    @posts = @blog.posts.order(:published_at).page(params[:page])
  end

  def new
    render
  end

  def create
    @post.save!
    flash[:success] = 'Blog Post created'
    redirect_to :action => :edit, :id => @post

  rescue ActiveRecord::RecordInvalid
    flash.now[:error] = 'Failed to create Blog Post'
    render :action => :new
  end

  def edit
    render
  end

  def update
    @post.update_attributes!(post_params)
    flash[:success] = 'Blog Post updated'
    redirect_to :action => :edit, :id => @post

  rescue ActiveRecord::RecordInvalid
    flash.now[:error] = 'Failed to update Blog Post'
    render :action => :edit
  end

  def destroy
    @post.destroy
    flash[:success] = 'Blog Post removed'
    redirect_to :action => :index
  end

protected

  def load_post
    @post = @blog.posts.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    flash[:error] = 'Blog Post not found'
    redirect_to :action => :index
  end

  def build_post
    @post = @blog.posts.new(post_params)
    @post.published_at ||= Time.zone.now
  end
  
  def post_params
    params.fetch(:post, {}).permit!
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comfy_blog-1.1.1 app/controllers/admin/blog/posts_controller.rb
comfy_blog-1.1.0 app/controllers/admin/blog/posts_controller.rb
comfy_blog-1.0.0 app/controllers/admin/blog/posts_controller.rb