简体   繁体   中英

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; when calling a controller using $.ajax({url: self.attr('href')}); and respond_to:js inside the controller it's just responding using 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')});

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.

What am I missing?

Your controller should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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