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

blog_posts_controller.rb 2.0KB

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