Replying to a request in ruby on rails (Server side)

302 views Asked by At

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?

1

There are 1 answers

0
gabrielhilal On BEST ANSWER

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