Created
February 16, 2016 01:12
-
-
Save armandofox/4fc27c7c8adec15f3ced to your computer and use it in GitHub Desktop.
reviews_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReviewsController < ApplicationController | |
before_filter :has_moviegoer_and_movie, :only => [:new, :create] | |
protected | |
def has_moviegoer_and_movie | |
unless @current_user | |
flash[:warning] = 'You must be logged in to create a review.' | |
redirect_to '/auth/twitter' | |
end | |
unless (@movie = Movie.find_by_id(params[:movie_id])) | |
flash[:warning] = 'Review must be for an existing movie.' | |
redirect_to movies_path | |
end | |
end | |
public | |
def new | |
@review = @movie.reviews.build | |
end | |
def create | |
# since moviegoer_id is a protected attribute that won't get | |
# assigned by the mass-assignment from params[:review], we set it | |
# by using the << method on the association. We could also | |
# set it manually with review.moviegoer = @current_user. | |
@current_user.reviews << @movie.reviews.build(params[:review]) | |
redirect_to movie_path(@movie) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment