Home>

[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.

【Current status】
-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)

【Questions】
-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 #<#: 0x000000000b570fc0>(NoMethodError)

<% 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
    reference
    https://qiita.com/h_tyokinuhata/items/809bdb66f8da6dd53bb6

  • 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)