A website template with lots of features, built with ruby on rails.

blog_posts_controller.rb 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. class BlogPostsController < ApplicationController
  2. before_filter :authenticate_user, only: [:edit, :update, :destroy]
  3. before_action :set_blog_post, only: [:show, :edit, :update, :destroy]
  4. # GET /blog_posts
  5. # GET /blog_posts.json
  6. def index
  7. @blog_posts = BlogPost.order('created_at DESC')
  8. end
  9. # GET /blog_posts/1
  10. # GET /blog_posts/1.json
  11. def show
  12. end
  13. # GET /blog_posts/new
  14. def new
  15. @blog_post = BlogPost.new
  16. @blog_post.published = true
  17. end
  18. # GET /blog_posts/1/edit
  19. def edit
  20. end
  21. # POST /blog_posts
  22. # POST /blog_posts.json
  23. def create
  24. @blog_post = BlogPost.new(blog_post_params)
  25. @blog_post.update(:author => current_user)
  26. respond_to do |format|
  27. if @blog_post.save
  28. format.html { redirect_to @blog_post, notice: 'Blog post was successfully created.' }
  29. format.json { render action: 'show', status: :created, location: @blog_post }
  30. else
  31. format.html { render action: 'new' }
  32. format.json { render json: @blog_post.errors, status: :unprocessable_entity }
  33. end
  34. end
  35. end
  36. # PATCH/PUT /blog_posts/1
  37. # PATCH/PUT /blog_posts/1.json
  38. def update
  39. respond_to do |format|
  40. if @blog_post.update(blog_post_params)
  41. format.html { redirect_to @blog_post, notice: 'Blog post was successfully updated.' }
  42. format.json { head :no_content }
  43. else
  44. format.html { }
  45. format.json { render json: @blog_post.errors, status: :unprocessable_entity }
  46. end
  47. end
  48. end
  49. # DELETE /blog_posts/1
  50. # DELETE /blog_posts/1.json
  51. def destroy
  52. @blog_post.destroy
  53. respond_to do |format|
  54. format.html { redirect_to blog_posts_url }
  55. format.json { head :no_content }
  56. end
  57. end
  58. private
  59. # Use callbacks to share common setup or constraints between actions.
  60. def set_blog_post
  61. @blog_post = BlogPost.friendly.find(params[:id])
  62. end
  63. # Never trust parameters from the scary internet, only allow the white list through.
  64. def blog_post_params
  65. params.require(:blog_post).permit(:title, :slug, :content, :published, :description, :image)
  66. end
  67. def authenticate_user
  68. redirect_to root_path, alert: (t 'admin_panel.permission_denied') unless current_user && current_user.admin?
  69. end
  70. end