Sha256: 220370874efe3e5825f8563e6b04076f21ca9048bab0b362b4dd6162c6837a32

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require_dependency 'mini_blog/application_controller'

module MiniBlog
  class ArticlesController < ApplicationController
    before_action :authenticate_user!, except: [:index, :show]

    def index
      @articles = Article.includes(:tags).page(params[:page]).per(10)
    end

    def show
      @article = Article.find(params[:id])
    end

    def new
      @article = Article.new
    end

    def create
      @article = Article.new(article_params)

      if @article.save
        redirect_to root_path
      else
        render 'new'
      end
    end

    def edit
      @article = Article.find(params[:id])
    end

    def update
      @article = Article.find(params[:id])

      if @article.update(article_params)
        redirect_to root_path
      else
        render 'edit'
      end
    end

    def destroy
      article = Article.find(params[:id])

      article.destroy

      redirect_to root_path
    end

  private
    def article_params
      params.require(:article).permit(:title, :body).merge(tags: params[:article][:tags].split(/;\s*/).map { |tag| MiniBlog::Tag.find_or_initialize_by(name: tag) })
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mini_blog-0.1.1 app/controllers/mini_blog/articles_controller.rb