Skip to content

Instantly share code, notes, and snippets.

@senny
Created February 6, 2013 10:30
Show Gist options
  • Save senny/4721746 to your computer and use it in GitHub Desktop.
Save senny/4721746 to your computer and use it in GitHub Desktop.
Rails 3.2.x scaffolded Controller
class ProductsController < ApplicationController
respond_to :html, :xml
def index
@products = Product.all
respond_with @products
end
def show
@product = Product.find(params[:id])
respond_with @product
end
def new
@product = Product.new
respond_with @product
end
def create
@product = Product.new(params[:product])
if @product.save
cookies[:last_product_id] = @product.id
flash[:notice] = "Successfully created product."
end
respond_with(@product)
end
def edit
@product = Product.find(params[:id])
respond_with(@product)
end
def update
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = "Successfully updated product."
end
respond_with(@product)
end
def destroy
@product = Product.find(params[:id])
@product.destroy
flash[:notice] = "Successfully destroyed product."
respond_with(@product)
end
end
class NotesController < ApplicationController
# GET /notes
# GET /notes.json
def index
@notes = Note.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @notes }
end
end
# GET /notes/1
# GET /notes/1.json
def show
@note = Note.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @note }
end
end
# GET /notes/new
# GET /notes/new.json
def new
@note = Note.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @note }
end
end
# GET /notes/1/edit
def edit
@note = Note.find(params[:id])
end
# POST /notes
# POST /notes.json
def create
@note = Note.new(params[:note])
respond_to do |format|
if @note.save
format.html { redirect_to @note, notice: 'Note was successfully created.' }
format.json { render json: @note, status: :created, location: @note }
else
format.html { render action: "new" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# PUT /notes/1
# PUT /notes/1.json
def update
@note = Note.find(params[:id])
respond_to do |format|
if @note.update_attributes(params[:note])
format.html { redirect_to @note, notice: 'Note was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note = Note.find(params[:id])
@note.destroy
respond_to do |format|
format.html { redirect_to notes_url }
format.json { head :no_content }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment