开发者

Rails 3 respond_to not responding

开发者 https://www.devze.com 2023-03-20 04:35 出处:网络
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 us

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
0

精彩评论

暂无评论...
验证码 换一张
取 消