ruby-on-rails 在select中选择硬币名称后显示其价格

x759pob2  于 2023-05-19  发布在  Ruby
关注(0)|答案(1)|浏览(100)

我使用slim-select,你可以选择硬币的名称,如何在选择名称后显示其价格(价格在数据库中,我可以通过选择中选择的名称找到它)我怎么做(也许我应该使用 AJAX 但如何)?
我的表单与select

<%= form_with(model: @investment, local: true) do |f| %>
      <div class="">
        <p class="very-mb-sm text-center"> <%= f.label :coin_name %></p>
        <%= f.select :coin_name, Coin.pluck(:name), {include_blank: true},{data: {controller: 'slim',class:'my-slim-select'}}%>
      </div>
      <p class="very-mb-sm very-mt-sm text-center"> <%= f.label :shopping_price %></p>
      <p class="text-center "><%= f.text_field :shopping_price, class: 'form-text-field'%></p>

      <p class="very-mb-sm very-mt-sm text-center"> <%= f.label :invest %></p>
      <p class="text-center"><%= f.text_field :invest, class: 'form-text-field'%></p>
        
      <div class="div-center mt-btn">
        <%= f.submit '+', class: "text-20" %>
      </div>
<% end %>

更新1
呈现的HTML

<div class="popup-content">
    <form action="/investments" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="u_oVXD2WK9CBd13gQq2i5XSDiEED389yjhY1Pv22sDj2fVz6Y2HJVwj2RRHGgzW9xcXSb78BYmjGFAhmMGOugA" autocomplete="off" />
      <div class="">
        <p class="very-mb-sm text-center"> <label for="investment_coin_name">Coin name</label></p>
        <select data-controller="slim" data-class="my-slim-select" name="investment[coin_name]" id="investment_coin_name"><option value="" label=" "></option>
<option value="Solana">Solana</option>
<option value="TRON">TRON</option>
<option value="Binance USD">Binance USD</option>
<option value="Dai">Dai</option>
<option value="Shiba Inu">Shiba Inu</option>
<option value="Litecoin">Litecoin</option>
<option value="Avalanche">Avalanche</option>
<option value="Uniswap">Uniswap</option>
<option value="Toncoin">Toncoin</option>
<option value="Wrapped Bitcoin">Wrapped Bitcoin</option>
<option value="Monero">Monero</option>
<option value="LEO Token">LEO Token</option>
<option value="TrueUSD">TrueUSD</option>
<option value="Ethereum Classic">Ethereum Classic</option>
<option value="OKB">OKB</option>
<option value="Chainlink">Chainlink</option>
<option value="Filecoin">Filecoin</option>
<option value="Cosmos Hub">Cosmos Hub</option>
<option value="Stellar">Stellar</option>
<option value="Lido DAO">Lido DAO</option>
<option value="Aptos">Aptos</option>
<option value="Bitcoin Cash">Bitcoin Cash</option>
<option value="Cronos">Cronos</option>
<option value="Quant">Quant</option>
<option value="Hedera">Hedera</option>
<option value="ApeCoin">ApeCoin</option>
<option value="The Graph">The Graph</option>
<option value="NEAR Protocol">NEAR Protocol</option>
<option value="Algorand">Algorand</option>
<option value="VeChain">VeChain</option>
<option value="Arbitrum">Arbitrum</option>
<option value="BNB">BNB</option>
<option value="Cardano">Cardano</option>
<option value="XRP">XRP</option>
<option value="Ethereum">Ethereum</option>
<option value="USD Coin">USD Coin</option>
<option value="Tether">Tether</option>
<option value="Polkadot">Polkadot</option>
<option value="Dogecoin">Dogecoin</option>
<option value="Lido Staked Ether">Lido Staked Ether</option>
<option value="Polygon">Polygon</option>
<option value="MultiversX">MultiversX</option>
<option value="Aave">Aave</option>
<option value="Radix">Radix</option>
<option value="Bitcoin">Bitcoin</option>
<option value="Internet Computer">Internet Computer</option>
<option value="Pax Dollar">Pax Dollar</option>
<option value="Fantom">Fantom</option>
<option value="EOS">EOS</option>
<option value="The Sandbox">The Sandbox</option></select>
</div>
cgh8pdjw

cgh8pdjw1#

我想如果你正在使用咖啡JS,那么我可以在这里提供帮助。
我假设你正在使用turbolinks:load来查看你的资产。
你可以创建这样的东西:

coinWithPrices: ->
    coin = $('#investment_coin_name').val()
    $.ajax(url : '/commons/get_prices', data :{ coin_name: coin })
    .then (data) ->
        selected_price = $('#investment_coin_price').data('coin_price')

$(document).on "turbolinks:load", ->
     coinWithPrices.init()

这是coffee js的一部分,现在一些工作在Rails上结束。

def get_prices
    @coin_name = params[:coin_name]
    data[:coin_price] = Investment.where("coin_name is ilike '#{@coin_name}'")
    render 'investment_form'
  end

我假设您的模型名称是Investment,您的表单名称是investment_form。
我想这对你有用。

相关问题