Advertisement
Guest User

Untitled

a guest
Jan 30th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. class MoviesController < ApplicationController
  2. before_action :set_movie, only: [:show, :edit, :update, :create_classification]
  3. before_action :authenticate_user!, except: [:index, :show]
  4.  
  5. def index
  6. @movies = Movie.all
  7. end
  8.  
  9. def show
  10. end
  11.  
  12. def edit
  13. end
  14.  
  15. def update
  16. if @movie.update(movie_params)
  17. redirect_to action: :show, id: @movie.id
  18. else
  19. render :edit, id: @movie.id
  20. end
  21. end
  22.  
  23. def new
  24. @movie = Movie.new
  25. end
  26.  
  27. def create
  28. @movie = Movie.new(movie_params)
  29. if @movie.save
  30. redirect_to action: :show, id: @movie.id
  31. else
  32. render :new
  33. end
  34. end
  35.  
  36. def create_classification
  37. classification = Classification.new(classification_params)
  38. classification.user = current_user
  39. classification.movie = @movie
  40. if classification.save
  41. flash[:notice] = "Score updated! "
  42. else
  43. flash[:alert] = "Could not update score. The score must be a number (0 - 10)"
  44. end
  45. redirect_to action: :show, id: @movie.id
  46. end
  47.  
  48. private
  49.  
  50. def movie_params
  51. params.require(:movie).permit(:title, :release_date, :description)
  52. end
  53.  
  54. def classification_params
  55. params.require(:classification).permit(:score)
  56. end
  57.  
  58. def set_movie
  59. id = params['id']
  60. @movie = Movie.find(id)
  61. rescue ActiveRecord::RecordNotFound
  62. render file: "#{Rails.root}/public/404.html", status: 404
  63. end
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement