Skip to content

Instantly share code, notes, and snippets.

@hyperbolic-motion
Created April 13, 2012 22:45
Show Gist options
  • Save hyperbolic-motion/2380619 to your computer and use it in GitHub Desktop.
Save hyperbolic-motion/2380619 to your computer and use it in GitHub Desktop.
Rails: basic crud controller conent
# --------------------------------------------------------------------------
# Basic rails CRUD controller snippet
# Find all:
# model => replace with the instance variable
# Model => replace with the class name
# --------------------------------------------------------------------------
def index
@models = Model.all
end
def new
@model = Model.new
end
def create
@model = Model.new(params[:model])
if @model.save
redirect_to models_url, notice: "Model successfully created."
else
render :new
end
end
def edit
@model = Model.find(params[:id])
end
def update
@model = Model.find(params[:id])
if @model.update_attributes(params[:model])
redirect_to models_url, notice: "Model successfully updated."
else
render :edit
end
end
def destroy
@model = Model.find(params[:id])
if @model.destroy
redirect_to models_url, notice: "Model successfully deleted."
else
redirect_to models_url, notice: "Not possible to destroy the Model."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment