Advertisement
saasbook

reviews_controller.rb

Jan 9th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.88 KB | None | 0 0
  1. class ReviewsController < ApplicationController
  2.   before_filter :has_moviegoer_and_movie, :only => [:new, :create]
  3.   protected
  4.   def has_moviegoer_and_movie
  5.     unless @current_user
  6.       flash[:warning] = 'You must be logged in to create a review.'
  7.       redirect_to login_path
  8.     end
  9.     unless (@movie = Movie.find_by_id(params[:movie_id]))
  10.       flash[:warning] = 'Review must be for an existing movie.'
  11.       redirect_to movies_path
  12.     end
  13.   end
  14.   public
  15.   def new
  16.     @review = @movie.reviews.build
  17.   end
  18.   def create
  19.     # since moviegoer_id is a protected attribute that won't get
  20.     # assigned by the mass-assignment from params[:review], we set it
  21.     # by using the << method on the association.  We could also
  22.     # set it manually with review.moviegoer = @current_user.
  23.     @current_user.reviews << @movie.reviews.build(params[:review])
  24.     redirect_to movie_path(@movie)
  25.   end
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement