[What I want to achieve]
-Receive JSON sent from the microcomputer board with API and display the result with View.
-The received JSON information is not saved in the DB immediately. After displaying it on the screen once, press the save button to save it in the DB.
-Display the loading screen for the time until it is displayed on the screen (currently GIF animation is displayed and supported by View).
-I want to move from loading to the result display screen when receiving JSON.
-Prepare global variables for each item received in JSON and initialize them with nil in advance.
-Parse the JSON received by the API and put the result in a global variable.
-The Controller checks every second whether the global variable is nil.
-If the global variable is not nil, I'm thinking of transitioning the screen.
-The check process is started in another thread, but it does not work (I am suffering from DoubleRenderError)
-In the first place, is there a method other than this method to make the screen transition when the API is received?
I think that the process of checking the value regularly using global variables is also a bad process.
(I am using it because I am reluctant to use global variables easily but there is no policy)
The code of the current Controller is as follows.
def show @id = params [: id] if @id == '2' render'err.html.erb' end th = Thread.new do → Check global variables in another thread while true do if $temp! = nil; th.kill ← Here I try to transition to the View prepared as the next screen with render, redirect, but it does not work break end sleep (1) puts'check' end end puts'checked' # Store the acquired value in an instance variable (to display it in View) @temp = $temp @id = params [: id] if @id == '3' render: layout =>'vital' end end
By writing the following code in View (erb), the check part has come to work.
However, the redirect_to part doesn't seem to work.
It seems that it can be solved by transitioning with JS or some other method, but I would like to borrow your wisdom.
undefined method `redirect_to'for #<#
<% th = Thread.new do%> <% while true do%> <% if $temp! = Nil;%> <% redirect_to'http: // localhost: 3000/checks/3'%> <% th.kill%> <% end%> <% sleep (1)%> <% puts'check'%> <% end%> <% end%>
Answer # 1
I think you need to use websocket.
Or poll with ajax
Answer # 2
Finally solved by the following method
-In Controller, send http GET request to device side with Ruby (separate thread)
-Determine whether the GET response is successful/unsuccessful
-If successful, hold the value in the instance variable, control ie with WIN32OLE and transition to the next screen (id difference in show action)
- ruby on rails - about screen transition from rails top page to calendar page
- ruby on rails 5 - i want to update the received parameters with nil
- ruby - i cannot transition to the new registration screen
- ruby on rails 5 - rails: create an administrator screen using activeadmin colors of other pages change
- ruby - i want to transition to the specified show screen
- ruby on rails - [rails] about the problem that screen transition is not possible (error does not occur)
- ruby on rails - how to transition from the top page
- ruby on rails - about the matter that does not transition to the detail page
- ruby - i can't transition to my page for each user after logging out (rails)
- ruby - i want to transition from create to show with rails
- ruby on rails - about switching screen display on multiple models
- [sqlite, ruby, rails] i want to sort the hash value data obtained from the table as i expected
- ruby on rails - not created
- ruby - [rails] how to resolve activerecord errors
- ruby - allow rails chat app to save to only one room
- ruby on rails - nomethoderror in itemscontroller # new error
- ruby on rails - [rails] i want to solve "no route matches [post]"/messages/search "" of "routing error&
- ruby - data is not updated by update action rails
- ruby - use form_with in rails to specify multiple models and save values
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- i want to check the type of a shell script variable