PageRenderTime 1211ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/blogs_controller.rb

https://gitlab.com/sulmanweb/Bloggy2
Ruby | 86 lines | 62 code | 10 blank | 14 comment | 3 complexity | 288e58cbc1332c325a16b7152dba168d MD5 | raw file
  1. class BlogsController < ApplicationController
  2. before_action :authenticate_user!, only: [:edit, :new, :update, :destroy]
  3. before_action :set_blog, only: [:show, :edit, :update, :destroy]
  4. # GET /blogs
  5. # GET /blogs.json
  6. def index
  7. if params[:q].present?
  8. @q = Blog.ransack(params[:q])
  9. @blogs = @q.result(distinct: true).order('created_at DESC')
  10. elsif params[:tag].present?
  11. @blogs = Blog.tagged_with(params[:tag]).order('created_at DESC')
  12. else
  13. @blogs = Blog.all.order('created_at DESC')
  14. end
  15. end
  16. # GET /blogs/1
  17. # GET /blogs/1.json
  18. def show
  19. end
  20. # GET /blogs/new
  21. def new
  22. @blog = Blog.new
  23. end
  24. # GET /blogs/1/edit
  25. def edit
  26. authorize @blog
  27. end
  28. # POST /blogs
  29. # POST /blogs.json
  30. def create
  31. @blog = Blog.new(blog_params)
  32. @blog.user = current_user
  33. respond_to do |format|
  34. if @blog.save
  35. format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
  36. format.json { render :show, status: :created, location: @blog }
  37. else
  38. format.html { render :new }
  39. format.json { render json: @blog.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43. # PATCH/PUT /blogs/1
  44. # PATCH/PUT /blogs/1.json
  45. def update
  46. authorize @blog
  47. respond_to do |format|
  48. if @blog.update(blog_params)
  49. format.html { redirect_to @blog, notice: 'Blog was successfully updated.' }
  50. format.json { render :show, status: :ok, location: @blog }
  51. else
  52. format.html { render :edit }
  53. format.json { render json: @blog.errors, status: :unprocessable_entity }
  54. end
  55. end
  56. end
  57. # DELETE /blogs/1
  58. # DELETE /blogs/1.json
  59. def destroy
  60. authorize @blog
  61. @blog.destroy
  62. respond_to do |format|
  63. format.html { redirect_to blogs_url, notice: 'Blog was successfully destroyed.' }
  64. format.json { head :no_content }
  65. end
  66. end
  67. private
  68. # Use callbacks to share common setup or constraints between actions.
  69. def set_blog
  70. @blog = Blog.friendly.find(params[:id])
  71. end
  72. # Never trust parameters from the scary internet, only allow the white list through.
  73. def blog_params
  74. params.require(:blog).permit(:title, :content, :publish, :tag_list, :image_url)
  75. end
  76. end