Advertisement
saasbook

controller_with_validation.rb

Jan 9th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.69 KB | None | 0 0
  1. # replaces the 'create' method in controller:
  2. def create
  3.   @movie = Movie.new(params[:movie])
  4.   if @movie.save
  5.     flash[:notice] = "#{@movie.title} was successfully created."
  6.     redirect_to movies_path
  7.   else
  8.     render 'new' # note, 'new' template can access @movie's field values!
  9.   end
  10. end
  11. # replaces the 'update' method in controller:
  12. def update
  13.   @movie = Movie.find params[:id]
  14.   if @movie.update_attributes(params[:movie])
  15.     flash[:notice] = "#{@movie.title} was successfully updated."
  16.     redirect_to movie_path(@movie)
  17.   else
  18.     render 'edit' # note, 'edit' template can access @movie's field values!
  19.   end
  20. end
  21. # as a reminder, here is the original 'new' method:
  22. def new
  23.   @movie = Movie.new
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement