[英]Rails 3 respond_to not responding
我试图解决这个问题,但我需要帮助!
我正在使用 rails 3 和 jquery,我包含 rails.js 并使用 gem 'jquery-rails' 来创建它 - 但仍然; 使用 $.ajax({url: self.attr('href')}) 调用 controller 时; 和 response_to:js 在 controller 中,它只是使用 html 响应。
Controller:
respond_to :html, :js
# GET /customer/new
def new
@customer = Customer.new
respond_with(@customer)
end
应用程序.js:
$.ajax({url: self.attr('href')});
起初我以为我设置正确的标题是一个问题,但在萤火虫中我可以看到 X-Requested-With 设置为 XMLHttpRequest - 但仍然 response_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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.