I want to know how to share the data returned by html and json in response_to.

Asked 1 years ago, Updated 1 years ago, 56 views

I am trying to display the data of each value and the graph using d3js on the index page, but if I write it like below,

on page display and json loading during graph drawing @foo processing to create the data.
After displaying html, @foo has the data, so how can I use it as it is for graph drawing?

def index
  @hoge=...
  @foo=@hoge....
  response_to do | format |
   format.html
   format.json {render:json=>@foo}
  end
end

ruby-on-rails json

2022-09-30 14:01

1 Answers

Basically, you can only respond once to a single request.

Two requests and responses are required.For example, here, two endpoints are required.
By all means

  • I want to display the contents of @foo in HTML
  • JSON But I want to return @foo

If that's the case, I think it's better to use volatile cache to make @foo in one time for these two requests.Keep it in memory for a certain amount of time.

Or, if HTML also shows the contents of @foo, one way is to stop ajax.


2022-09-30 14:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.