Last active
August 29, 2015 13:58
-
-
Save tcannonfodder/9939717 to your computer and use it in GitHub Desktop.
Basic Controller
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
def new | |
@model = Model.new | |
@Title = "New Model" | |
end | |
def create | |
@model = Model.new(params[:model]) | |
if @model.save | |
redirect_to model_path(@model), :flash => {:success => "Model Created"} | |
else | |
render 'new' | |
end | |
end | |
def index | |
@models = Model.all | |
@Title = "Models" | |
end | |
def show | |
@Title = "Model Details" | |
begin | |
@model = Model.find(params[:id]) | |
rescue ActiveRecord::RecordNotFound | |
redirect_to root_path, :flash => {:notice => "Model not found"} | |
end | |
end | |
def edit | |
@Title = "Edit Model" | |
begin | |
@model = Model.find(params[:id]) | |
rescue ActiveRecord::RecordNotFound | |
redirect_to root_path, :flash => {:notice => "Model not found"} | |
end | |
end | |
def update | |
begin | |
@model = Model.find(params[:id]) | |
if @model.update_attributes(params[:model]) | |
render 'show' | |
else | |
render 'edit' | |
end | |
rescue ActiveRecord::RecordNotFound | |
redirect_to root_path, :flash => {:notice => "Model not found"} | |
end | |
end | |
def destroy | |
begin | |
@model = Model.find(params[:id]) | |
@model.destroy | |
redirect_to root_path, :flash => {:success => "Model Deleted"} | |
rescue ActiveRecord::RecordNotFound | |
redirect_to root_path, :flash => {:error => "Model not found"} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment