1

我有一个奇怪的问题。每当我执行 AJAX 请求时,我都会收到关于实例变量为“nil”的错误。似乎请求没有从控制器得到任何东西。

这已经在其他 PC 上工作了,我现在正在安装新的 ubuntu。有些设置可能是错误的。

我通过这个js函数。

jQuery.fn.mark_todo_done = function (){
  this.live('click', function() { 
    $('.spinning').show();
    var todo_id =  $(this).attr("id");

  $.getScript("/mark_todo_done/" + todo_id)
    })
};

它去这条路线 routes.rb

match "/mark_todo_done/:id" => "private#mark_todo_done", :as => :mark_todo_done

而这种方法在

私人控制器

def mark_todo_done
    @firm = current_user.firm
    @todo = Todo.find(params[:id])
    @project = @todo.project
    if @todo.completed == true
      @todo.completed = false
    else
      @todo.completed = true
    end
    @todo.update_attributes(params[:todo])
    @done_todos = @project.todos.where(["completed = ?", true]).includes(:user)
    @not_done_todos = @project.todos.where(["completed = ?", false]).includes(:user) 
    respond_to do |format|
      format.js
    end
  end

我收到此错误消息

ActionView::Template::Error (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
    1: 
    2: $("#todo_<%= escape_javascript( @todo.id.to_s) %>").remove();
    3: <% if @todo.completed == true %>
    4: $("#done_tasks").append("<%= escape_javascript(render(:partial => @todo)) %>");
    5: 
  app/views/private/mark_todo_done.js.erb:2:in `_app_views_private_mark_todo_done_js_erb___267824823023937232_51228900'

我在所有 AJAX 请求上都收到类似的错误。有任何想法吗?

我的 application.js 中也有这个

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
4

1 回答 1

1

解决方案

在私人控制器中摆脱对私人的调用

#private
def mark_todo_done
    @firm = current_user.firm
    @todo = Todo.find(params[:id])
    @project = @todo.project
    if @todo.completed == true
      @todo.completed = false
    else
      @todo.completed = true
    end
    @todo.update_attributes(params[:todo])
    @done_todos = @project.todos.where(["completed = ?", true]).includes(:user)
    @not_done_todos = @project.todos.where(["completed = ?", false]).includes(:user) 
    respond_to do |format|
      format.js
    end
  end

希望它可以帮助某人...

于 2012-04-12T20:55:52.847 回答