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

blog_posts_controller.rb 2.3KB

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