Skip to content

Instantly share code, notes, and snippets.

@dfee
Created May 2, 2010 21:29
Show Gist options
  • Save dfee/387467 to your computer and use it in GitHub Desktop.
Save dfee/387467 to your computer and use it in GitHub Desktop.
class TasksController < ApplicationController
# GET /tasks
# GET /tasks.xml
def index
@tasks = Task.all
respond_to do |format|
tasks = @tasks.map {|task| json_for_task(task) }
format.json { render :json => { :content => tasks } }
format.html
format.xml { render :xml => @tasks }
end
end
# GET /tasks/1
# GET /tasks/1.xml
def show
@task = Task.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @task }
format.json do
render :json => {
:content => json_for_task(@task),
:location => task_path(@task)
}
end
end
end
# GET /tasks/new
# GET /tasks/new.xml
def new
@task = Task.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @task }
end
end
# GET /tasks/1/edit
def edit
@task = Task.find(params[:id])
end
# POST /tasks
# POST /tasks.xml
def create
@task = Task.new(params[:task])
respond_to do |format|
if @task.save
flash[:notice] = 'Task was successfully created.'
format.json do
render :json => { :content => json_for_task(@task) }, :status => :created,
:location => task_path(@task)
end
format.html { redirect_to(@task) }
format.xml { render :xml => @task, :status => :created, :location => @task }
else
format.html { render :action => "new" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
end
# PUT /tasks/1
# PUT /tasks/1.xml
def update
@task = Task.find(params[:id])
respond_to do |format|
params[:task].delete(:guid)
if @task.update_attributes(params[:task])
flash[:notice] = 'Task was successfully updated.'
format.json do
render :json => { :content => json_for_task(@task) }, :location => task_path(@task)
end
format.html { redirect_to(@task) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.xml
def destroy
@task = Task.find(params[:id])
@task.destroy
respond_to do |format|
format.json { head :ok }
format.html { redirect_to(tasks_url) }
format.xml { head :ok }
end
end
protected
def json_for_task(task)
{ :guid => task_path(task),
:description => task.description,
:isDone => task.isDone
}
end
end
@dfee
Copy link
Author

dfee commented May 2, 2010

Processing TasksController#update (for 127.0.0.1 at 2010-05-02 16:27:57) [PUT]
Parameters: {"isDone"=>false, "id"=>"10", "description"=>"jkjkj"}
Task Load (0.2ms) SELECT * FROM "tasks" WHERE ("tasks"."id" = 10)

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.delete):
app/controllers/tasks_controller.rb:75:in update' app/controllers/tasks_controller.rb:74:inupdate'

Rendered rescues/_trace (73.4ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (internal_server_error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment