我试图解决这个问题,但我需要帮助!
我正在使用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/