0

I am making a webb app using rails. At one point I am sending a request to the server to get some information in an array. I can handle the request on the server side, using routing and controllers properly. But once the server has done its math, I don't know how to send a reply back to the front end.

How do you send information back to the front end using rails?

Daniel Hernandez
  • 635
  • 1
  • 11
  • 22
  • you can send it as json – Vrushali Pawar Jun 08 '15 at 12:24
  • The process of creating a response and sending it is called **rendering**. Normally you would render an html page and send that, but you can return other types of data too, such as JSON, which is a good choice for sending an array back to the client. You need to read some docs, eg http://guides.rubyonrails.org/layouts_and_rendering.html – Max Williams Jun 08 '15 at 12:25
  • Ultimately you are probably going to say something like `render json: @my_array`. Your request will need to be a request for json instead of html. Ajax is a good technology for this as it's likely you will want to use that single array to change a part of the page, not do a total reload. – Max Williams Jun 08 '15 at 12:29

1 Answers1

0

Once the server has done its math (in the controller), you can return your array like the below:

def my_action
  my_array = # store the array here
  render json: my_array
end

You can also have the response in different formats..

def my_action
  my_array = # store the array here
  respond_to do |format|
    # ...
    format.json { render json: my_array }
  end
end
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81