Skip to content

Instantly share code, notes, and snippets.

@evantravers
Created July 8, 2011 16:07
Show Gist options
  • Save evantravers/1072152 to your computer and use it in GitHub Desktop.
Save evantravers/1072152 to your computer and use it in GitHub Desktop.
class TodosController < ApplicationController
# GET /todos
# GET /todos.json
def index
@todos = current_user.lists.find(params[:list_id]).todos
respond_to do |format|
format.html # index.html.erb
format.json { render json: @todos }
end
end
# GET /todos/1
# GET /todos/1.json
def show
@todo = Todo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @todo }
end
end
# GET /todos/new
# GET /todos/new.json
def new
# TODO this has to be changed
@list = List.find(params['list_id'])
@todo = @list.todos.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @todo }
end
end
# GET /todos/1/edit
def edit
@todo = Todo.find(params[:id])
end
# POST /todos
# POST /todos.json
def create
@todo = Todo.new(params[:todo])
respond_to do |format|
if @todo.save
format.html { redirect_to @todo, notice: 'Todo was successfully created.' }
format.json { render json: @todo, status: :created, location: @todo }
else
format.html { render action: "new" }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
# PUT /todos/1
# PUT /todos/1.json
def update
@todo = Todo.find(params[:id])
respond_to do |format|
if @todo.update_attributes(params[:todo])
format.html { redirect_to @todo, notice: 'Todo was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1
# DELETE /todos/1.json
def destroy
@todo = Todo.find(params[:id])
@todo.destroy
respond_to do |format|
format.html { redirect_to todos_url }
format.json { head :ok }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment