Created
April 13, 2012 22:45
-
-
Save hyperbolic-motion/2380619 to your computer and use it in GitHub Desktop.
Rails: basic crud controller conent
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
# -------------------------------------------------------------------------- | |
# 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