Skip to content

Instantly share code, notes, and snippets.

@tcannonfodder
Last active August 29, 2015 13:58
Show Gist options
  • Save tcannonfodder/9939717 to your computer and use it in GitHub Desktop.
Save tcannonfodder/9939717 to your computer and use it in GitHub Desktop.
Basic Controller
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