How to make an ajax request that returns a text in html?

2

Assuming the link making the request is:

<%= link_to 'New Classroom', new_classroom_path,class: :remote_link, remote: true %>

And that the return processing of it is:

$('.remote_link').bind('ajax:success',function(e, data, status, xhr) {
    ajax_replace(data.responseText, status, xhr)
  });

function ajax_replace(data, textStatus, request)
{
    replace_html(data,request.getResponseHeader("content_id"));
}

In which is the html text returned replaces the html text found in the div that has the same id as the one stored in the "content_id" header

The method that requests the server is:

after_filter :set_featured_id

def new
    @classroom = Classroom.new
    respond_to do |format|
      format.js{render :new, formats: [:html]; head :ok}
    end  
end 

def set_featured_id
    response.headers['content_id'] = 'featured'
end

In which a method includes the required value in the header after the execution of the treatment.

The problem is that although the value set in the header is retrieved correctly, the rendered value is not found as responseText.

    
asked by anonymous 05.05.2015 / 01:19

1 answer

1

You do not need to use the js format, use the html and leave the layout as false

#exemplo
format.html{ render '/products/new', layout: false } if request.xhr?

When you get the answer from the server it will send exactly the content that is in new.html.erb, but the layout of the page, if I understand is what you want right?

    
18.12.2015 / 13:58