Skip to content

Instantly share code, notes, and snippets.

@armandofox
Created February 22, 2021 00:59
Show Gist options
  • Save armandofox/579c3846f57f8133cd6e1953ef259c74 to your computer and use it in GitHub Desktop.
Save armandofox/579c3846f57f8133cd6e1953ef259c74 to your computer and use it in GitHub Desktop.
controller_forms_methods.rb
class MoviesController < ApplicationController
# 'index' and 'show' methods from Section 4.4 omitted for clarity
def new
@movie = Movie.new
end
def create
if (@movie = Movie.create(movie_params))
redirect_to movies_path, :notice => "#{@movie.title} created."
else
flash[:alert] = "Movie #{@movie.title} could not be created: " +
@movie.errors.full_messages.join(",")
render 'new'
end
end
def edit
@movie = Movie.find params[:id]
end
def update
@movie = Movie.find params[:id]
if (@movie.update_attributes(movie_params))
redirect_to movie_path(@movie), :notice => "#{@movie.title} updated."
else
flash[:alert] = "#{@movie.title} could not be updated: " +
@movie.errors.full_messages.join(",")
render 'edit'
end
end
def destroy
@movie = Movie.find(params[:id])
@movie.destroy
redirect_to movies_path, :notice => "#{@movie.title} deleted."
end
private
def movie_params
params.require(:movie)
params[:movie].permit(:title,:rating,:release_date)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment