How to request html page using ajax request in Rails 3?

I am trying Rails to render a template .html.erbusing an ajax request under certain conditions. By default, I return the file .js.erbwhen I send an ajax request.

I will not go into why I am doing this, but I was wondering if there is a way to indicate when I send the request via ajax that I would like to use html content and not js, OR alternatively if there is a way to refactor the following in Rails 3.1:

respond_to do |format|
  format.html
  format.js do
    if params[:page].nil?
      render "home.html.erb"
    else
      render "home.js.erb"
    end
  end
end

Thank!

+3
source share
1 answer

Assuming you are using jquery, you can set the dataType option, i.e.

$.ajax('some/page', {dataType: 'html'})

Or

$.get('some/page', function(){ //callback}, 'html')

URL (.. request '/some/page.html) ( , )

+3

All Articles