Call API from Rails

Call API from Rails

本文关键字:Rails from API Call      更新时间:2023-09-26

我想从我的web应用程序中使用我自己的API。

这或多或少是我想做的,但在我的API的控制器中:

在前端两个值是由用户引入的,这两个值应该被发送到API,然后在我的控制器中进行一些计算,最后我在我的视图中得到结果。我已经这样做了,但让我们说在正常的控制器(不是api/v1中的那个)。

我的index.html.erb是:

<div >
  <p class="mod1_title">Mein Lastprofil berechnen</p>
  <div class="mod1_boxed">
    <strong>Jahreshausverbrauch (kWh):</strong>
      <%= form_tag( '/calculation/index', post: true ) do %>
      <%= text_field_tag "user_entry_module1", nil, placeholder: "Bsp. 3500", id: "user_entry" %></br>
      <strong>PV-Große (kWp):</strong></br>
      <%= text_field_tag "user_entry_module2", nil, placeholder: "Bsp. 5", id: "user_entry_2" %></br>
      <%= submit_tag "Berechnen"%>
      <%end%>
  </div>  
</div>

Ergebnis: <%= @a %> 

谁能告诉我如何实现这一点?

从我的Rails应用程序调用API。我使用javascript的AJAX调用。