我试图解决这个问题,但我需要帮助!
我正在使用rails 3和jquery,我有rails.js包含并使用gem'jquery-rails'创建它 - 但仍然;使用$ .ajax({url:self.attr('href')})调用控制器时;和respond_to:js在控制器里面它只是用html响应。
控制器:
respond_to :html, :js
# GET /customer/new
def new
@customer = Customer.new
respond_with(@customer)
end
的application.js:
$.ajax({url: self.attr('href')});
起初我认为设置正确的标题是一个问题,但在firebug中我可以看到X-Requested-With设置为XMLHttpRequest - 但仍然是respond_with返回html而不是js。
我错过了什么?
答案 0 :(得分:0)
您的控制器应如下所示:
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