jquery - Rails 3 respond_to 不响应

标签 jquery ruby-on-rails

我试图解决这个问题,但我需要帮助!

我正在使用rails 3和jquery,我包含rails.js并使用gem'jquery-rails'来创建它 - 但仍然;使用 $.ajax({url: self.attr('href')}) 调用 Controller 时; Controller 内的 respond_to :js 只是使用 html 进行响应。

Controller :

respond_to :html, :js

  # GET /customer/new
  def new
    @customer = Customer.new

    respond_with(@customer)
  end

application.js:

$.ajax({url: self.attr('href')});

起初我以为这是设置正确 header 的问题,但在 firebug 中我可以看到 X-Requested-With 设置为 XMLHttpRequest - 但仍然 respond_with 返回 html 而不是 js。

我错过了什么?

最佳答案

您的 Controller 应如下所示:

def new
  @customer = Customer.new
  respond_to do |format|
    format.html # just renders like it normally would
    format.js do
      # Do whatever you need to do.
      # By default it would render /customer/new.js.erb.
      #
      # If you're rendering some html from a view here, you'll want to
      # tell rails not to render the layout by saying: render :layout => false
    end
  end
end

关于jquery - Rails 3 respond_to 不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6697853/

相关文章:

ruby-on-rails - 最常见元素的ruby数组计数

ruby-on-rails - 用自定义图像替换损坏的外部图像

ruby-on-rails - 仅当您成功保存父对象时才保存对 has_many 关联的更改?

javascript - jqGrid showCol/hideCol 排序后不起作用

jquery - 滚动魔术固定和 HTML 5 视频标签

javascript - jQuery Cycle 插件问题

ruby-on-rails - 为什么 Rails 试图重定向?

jquery - 如何获取 Accordion jQuery 的 Header id 和 Div id?

jquery - 使用 jquery 和 ajax 发送授权 header

javascript - 如何在 javascript 中使用 ruby​​ 代码