Rails 3 - Ajax Put请求给出404,但它可以工作

时间:2011-09-02 02:32:35

标签: ruby-on-rails ruby-on-rails-3

我正在从交易概览页面更新任务状态,我在该页面中显示该交易的任务列表

它有效,任务得到更新。但是,我得到了一个令人讨厌的404错误。

有什么想法吗?

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"

我的firebug输出

http://img707.imageshack.us/img707/7899/screenshot20110901at102.png

如果这不是“正确的方法”,我愿意接受建议。虽然要求是必须用ajax完成。

提前致谢!

编辑:请求的信息

路由错误

<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>}

路由

  resources :accounts do
resources :users
resources :deals do
  post :sort, on: :member
  resources :tasks do
    post :sort, on: :collection 
  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

耙/ 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"}

2 个答案:

答案 0 :(得分:2)

您需要将帐户添加到重定向,例如

# 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], ...) }

此外,正如评论中所述,您的Ajax请求不是要求javascript,而是要求HTML。你需要做Kristian PD所说的并将dataType: script放到指定JS的选项中,例如。

$.ajax 
  url: "tasks/" + task_id
  data: "&task[is_completed]=2"
  type: "PUT"
  dataType: "script"
  success: ->
    $(this).addClass "done"

答案 1 :(得分:2)

尝试

 $.ajax 
      url: "tasks/" + task_id
      dataType: "script"
      data: "&task[is_completed]=2"
      type: "PUT"
      success: ->
        $(this).addClass "done"

我认为它不会呈现您的JS响应,并且它正在尝试使用HTML重定向(也存在数字所指出的问题)