【问题标题】:Rails 3 respond_to not respondingRails 3 respond_to 没有响应
【发布时间】:2011-07-14 18:03:44
【问题描述】:

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

我正在使用 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')});

起初我以为我设置正确的标头是一个问题,但在萤火虫中我可以看到 X-Requested-With 设置为 XMLHttpRequest - 但仍然 response_with 返回 html 而不是 js。

我错过了什么?

【问题讨论】:

    标签: jquery ruby-on-rails


    【解决方案1】:

    您的控制器应如下所示:

    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
    

    【讨论】:

    • 为什么当我在顶部使用 respond_to 和 respond_with 时,这两个应该为我处理好。我正在使用 rails 3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多