How to make asynchronous GET request on RAILS and only get view form (not whole body and headers)

Viktor Dunslow

I created a simple asynchronous jquery function on my rails app so I can print the response on a "display" div. But when I make the GET request to my rails url it responds the whole html: "body", "head" and "includes" as if it were a new page on a browser, so the resulting source is the following:

Before I push the button:

<html>
  <head>
    <title>Myapp1</title>
  </head>
  <body>
      <button onclick="callPage()">Call</button>
      <div id="display"></div>
  </body>
<html>

After I push the button and the "display" div is filled with my response:

<html>
      <head>
        <title>Myapp1</title>

      </head>
      <body>
          <button onclick="callPage()">Call</button>
          <div id="display">
             *<html>
               <head>
                 <title>Myapp1</title>
               </head>
               <body>
                 -----Content of page user/1 which is the only thing I want to get.------
               </body>
             <html>*
          </div>
      </body>

My route to users on routes.rb (RUBY ON RAILS) working fine

  resources :users
  #get 'welcome/index'
  match ':controller(/:action(/:id))', :via => :get

My Jquery GET callPage (working fine)

<script>
         function callPage()
    {   
        url="user/1";
        div="display";
        $.ajax({
            type: "GET",
            dataType: "text",
            url: url,
            success: function(msg){
                if(parseInt(msg)!=0)
                {
                    $("#"+div).html(msg);
                }
            }   
        }).setRequestHeader('X-CSRF-Token', AUTH_TOKEN);
    }
        </script> 

The question is how do I prevent RAILS from printing out every html tag all over (body, heads,...) and get it to print only the view of the controller I want (in this case, users). ¿Is it a configuration somewhere or do I wave to trim this off somehow?

Thanks a million in advance for your time.

nbermudezs

To only display the view you have to change your controller action to have render layout: false at the end. This will prevent the layout from rendering, leaving only the content in your view file.

For example:

class YourController < ApplicationController
  def show
    # bla bla
    render 'your_show_view_name', layout: false
  end
end

If you want all your controller actions to have this behavior you can put it right at the top

class YourController < ApplicationController
  layout false
  def show
    # bla bla
  end
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get response headers and body in dispatch request?

From Dev

How to get only Message Body from a GET HTTP request?

From Dev

Http POST request get headers and body

From Dev

How do I make a GET request with JSON in the request body

From Dev

How to get GET request body

From Dev

Make Asynchronous GET/POST request in Swift

From Dev

Make Asynchronous GET/POST request in Swift

From Dev

How to get div to take up the whole of the body?

From Dev

Make a http get request with headers in Java

From Dev

Can only make one GET request without error: "Can't set headers after they are sent."

From Dev

Can only make one GET request without error: "Can't set headers after they are sent."

From Dev

How to use a form to make a get request with header information

From Dev

how to make ajax request to get the two different view in one template

From Java

how to get curl to output only http response body (json) and no other headers etc

From Java

How to get request headers to be saved to MDC

From Dev

How to add `headers` to my get `request`.

From Dev

How to get request headers using Mechanize?

From Dev

How to get request headers to be saved to MDC

From Dev

How to get request headers using Mechanize?

From Dev

How to get request headers from the CefSharp browser?

From Dev

How to get full HTTP request (not response) headers

From Dev

Laravel get request headers

From Dev

How to get Rails to respond with json based on headers

From Dev

How to get only response headers from XMLHttpRequest

From Dev

Powerbuilder GET request with request headers

From Dev

EssentialAction: How to Get the Body of a Request without Parsing It

From Dev

how to get request body text using bottle?

From Dev

How to get the Body from a request, using httptools?

From Java

HTTP GET with request body

Related Related

  1. 1

    How to get response headers and body in dispatch request?

  2. 2

    How to get only Message Body from a GET HTTP request?

  3. 3

    Http POST request get headers and body

  4. 4

    How do I make a GET request with JSON in the request body

  5. 5

    How to get GET request body

  6. 6

    Make Asynchronous GET/POST request in Swift

  7. 7

    Make Asynchronous GET/POST request in Swift

  8. 8

    How to get div to take up the whole of the body?

  9. 9

    Make a http get request with headers in Java

  10. 10

    Can only make one GET request without error: "Can't set headers after they are sent."

  11. 11

    Can only make one GET request without error: "Can't set headers after they are sent."

  12. 12

    How to use a form to make a get request with header information

  13. 13

    how to make ajax request to get the two different view in one template

  14. 14

    how to get curl to output only http response body (json) and no other headers etc

  15. 15

    How to get request headers to be saved to MDC

  16. 16

    How to add `headers` to my get `request`.

  17. 17

    How to get request headers using Mechanize?

  18. 18

    How to get request headers to be saved to MDC

  19. 19

    How to get request headers using Mechanize?

  20. 20

    How to get request headers from the CefSharp browser?

  21. 21

    How to get full HTTP request (not response) headers

  22. 22

    Laravel get request headers

  23. 23

    How to get Rails to respond with json based on headers

  24. 24

    How to get only response headers from XMLHttpRequest

  25. 25

    Powerbuilder GET request with request headers

  26. 26

    EssentialAction: How to Get the Body of a Request without Parsing It

  27. 27

    how to get request body text using bottle?

  28. 28

    How to get the Body from a request, using httptools?

  29. 29

    HTTP GET with request body

HotTag

Archive