简体   繁体   English

Rails 3 respond_to 没有响应

[英]Rails 3 respond_to not responding

I trying to get my head around this issue but I need help!我试图解决这个问题,但我需要帮助!

I am using rails 3 and jquery, I have rails.js included and used gem 'jquery-rails' to create it - but still;我正在使用 rails 3 和 jquery,我包含 rails.js 并使用 gem 'jquery-rails' 来创建它 - 但仍然; when calling a controller using $.ajax({url: self.attr('href')});使用 $.ajax({url: self.attr('href')}) 调用 controller 时; and respond_to:js inside the controller it's just responding using html.和 response_to:js 在 controller 中,它只是使用 html 响应。

Controller: Controller:

respond_to :html, :js

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

    respond_with(@customer)
  end

application.js:应用程序.js:

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

At first I thought I it was an issue with setting the correct headers, but in firebug I can see that the X-Requested-With is set to XMLHttpRequest - but still respond_with returns html and not js.起初我以为我设置正确的标题是一个问题,但在萤火虫中我可以看到 X-Requested-With 设置为 XMLHttpRequest - 但仍然 response_with 返回 html 而不是 js。

What am I missing?我错过了什么?

Your controller should look like this:您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM