Created
February 16, 2016 01:12
-
-
Save armandofox/298eb16fe86a9e693151 to your computer and use it in GitHub Desktop.
controller_with_validation.rb
This file contains 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
# replaces the 'create' method in controller: | |
def create | |
@movie = Movie.new(params[:movie]) | |
if @movie.save | |
flash[:notice] = "#{@movie.title} was successfully created." | |
redirect_to movies_path | |
else | |
render 'new' # note, 'new' template can access @movie's field values! | |
end | |
end | |
# replaces the 'update' method in controller: | |
def update | |
@movie = Movie.find params[:id] | |
if @movie.update_attributes(params[:movie]) | |
flash[:notice] = "#{@movie.title} was successfully updated." | |
redirect_to movie_path(@movie) | |
else | |
render 'edit' # note, 'edit' template can access @movie's field values! | |
end | |
end | |
# note, you will also have to update the 'new' method: | |
def new | |
@movie = Movie.new | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment