Rails 3.2.1缺少AJAX响应的即时变量; Ruby 1.9.3

时间:2012-04-12 20:13:37

标签: ruby-on-rails ajax

我遇到了一个奇怪的问题。每当我执行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")}
})

1 个答案:

答案 0 :(得分: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

希望它有助于somone ...