Rails 3 - Ajax Put request gives 404, yet it works
I am updating a task status from my deal overview page where i am displaying my list of tasks for that deal
It works, the task gets updated. However, i get a nasty 404 error.
Any ideas?
js/coffeescript
jQuery ->
$(".complete_task").live "click", ->
task_id = $(this).attr("value")
alert "checkbox clicked" + task_id
$('ul#tasks').sortable "refresh"
$.ajax
url: "tasks/" + task_id
data: "&task[is_completed]=2"
type: "PUT"
success: ->
$(this).addClass "done"
开发者_开发问答
My firebug output
http://img707.imageshack.us/img707/7899/screenshot20110901at102.png
I'm open to suggestions if this is not the "right way" to do it. Though the requirement is that it must be done with ajax.
Thanks in advance!
EDIT: Requested Information
Routing Error
<h1>Routing Error</h1>
No route matches {:controller=>"tasks", :action=>"show", :id=>#<Deal id: 1, title: "4919 Torrey Pines Run", user_id: nil, created_at: "2011-09-02 00:08:24", updated_at: "2011-09-02 00:08:24", account_id: 1>, :format=>#<Task id: 39, title: "new tdsfsdf", position: 25, user_id: nil, deal_id: 1, created_at: "2011-09-02 02:49:09", updated_at: "2011-09-02 02:50:31", is_completed: 2, user_who_completed: nil>}
routes
resources :accounts do
resources :users
resources :deals do
post :sort, on: :member
resources :tasks do
post :sort, on: :collection
end
end
end
respond_to block
def update
respond_to do |format|
if @task.update_attributes(params[:task])
format.html { redirect_to([@task.deal, @task], :notice => 'Task was successfully updated.') }
format.xml { head :ok }
format.js { render :nothing => true }
else
format.html { render :action => "edit" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
format.js { render :nothing => true }
end
end
end
rake/grep
MacBook-Pro-2:accountable jbeasley6651$ rake routes | grep tasks
sort_account_deal_tasks POST /accounts/:account_id/deals/:deal_id/tasks/sort(.:format) {:action=>"sort", :controller=>"tasks"}
account_deal_tasks GET /accounts/:account_id/deals/:deal_id/tasks(.:format) {:action=>"index", :controller=>"tasks"}
POST /accounts/:account_id/deals/:deal_id/tasks(.:format) {:action=>"create", :controller=>"tasks"}
new_account_deal_task GET /accounts/:account_id/deals/:deal_id/tasks/new(.:format) {:action=>"new", :controller=>"tasks"}
edit_account_deal_task GET /accounts/:account_id/deals/:deal_id/tasks/:id/edit(.:format) {:action=>"edit", :controller=>"tasks"}
account_deal_task GET /accounts/:account_id/deals/:deal_id/tasks/:id(.:format) {:action=>"show", :controller=>"tasks"}
PUT /accounts/:account_id/deals/:deal_id/tasks/:id(.:format) {:action=>"update", :controller=>"tasks"}
DELETE /accounts/:account_id/deals/:deal_id/tasks/:id(.:format) {:action=>"destroy", :controller=>"tasks"}
You need to add the account to your redirect, e.g.
# adding it as @account, change to however you're referencing it.
# params[:account_id] should also work.
format.html { redirect_to([@account, @task.deal, @task], ...) }
Also, as noted in the comments, your Ajax request isn't asking for javascript, but rather HTML. You'd need to do what Kristian PD says and put dataType: script
to the options to specify JS, e.g.
$.ajax
url: "tasks/" + task_id
data: "&task[is_completed]=2"
type: "PUT"
dataType: "script"
success: ->
$(this).addClass "done"
Try
$.ajax
url: "tasks/" + task_id
dataType: "script"
data: "&task[is_completed]=2"
type: "PUT"
success: ->
$(this).addClass "done"
I don't think it's rendering your JS response and it's trying to use the HTML redirect (which also has problems as noted by numbers)
精彩评论