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

blog_posts_controller.rb 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.all
  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. end
  17. # GET /blog_posts/1/edit
  18. def edit
  19. end
  20. # POST /blog_posts
  21. # POST /blog_posts.json
  22. def create
  23. @blog_post = BlogPost.new(blog_post_params)
  24. @blog_post.update(:author => current_user)
  25. respond_to do |format|
  26. if @blog_post.save
  27. format.html { redirect_to @blog_post, notice: 'Blog post was successfully created.' }
  28. format.json { render action: 'show', status: :created, location: @blog_post }
  29. else
  30. format.html { render action: 'new' }
  31. format.json { render json: @blog_post.errors, status: :unprocessable_entity }
  32. end
  33. end
  34. end
  35. # PATCH/PUT /blog_posts/1
  36. # PATCH/PUT /blog_posts/1.json
  37. def update
  38. respond_to do |format|
  39. if @blog_post.update(blog_post_params)
  40. format.html { redirect_to @blog_post, notice: 'Blog post was successfully updated.' }
  41. format.json { head :no_content }
  42. else
  43. format.html { }
  44. format.json { render json: @blog_post.errors, status: :unprocessable_entity }
  45. end
  46. end
  47. end
  48. # DELETE /blog_posts/1
  49. # DELETE /blog_posts/1.json
  50. def destroy
  51. @blog_post.destroy
  52. respond_to do |format|
  53. format.html { redirect_to blog_posts_url }
  54. format.json { head :no_content }
  55. end
  56. end
  57. private
  58. # Use callbacks to share common setup or constraints between actions.
  59. def set_blog_post
  60. @blog_post = BlogPost.friendly.find(params[:id])
  61. end
  62. # Never trust parameters from the scary internet, only allow the white list through.
  63. def blog_post_params
  64. params.require(:blog_post).permit(:title, :slug, :content, :published, :description, :image)
  65. end
  66. def authenticate_user
  67. redirect_to root_path, alert: 'You dont have permission to access that page!' unless current_user && current_user.admin?
  68. end
  69. end