结果在 rails 上用 ruby 重复了几次消息
The result repeats the message several times with ruby on rails
我有这个代码:
<% @lookup_coins.each do |x| %>
<% if @symbol == '' %>
<%= 'Sorry, but you forgot to write something, LOL'%>
<% elsif @symbol == x %>
<%= x["symbol"]%> <br/> <%= x["name"]%>: <%= number_to_currency(x['quote']['USD']['price'].round(2) , :unit => "$ ") %> <br/> <%= x["cmc_rank"]%> <br/>
<% else 'Sorry, there was a mistake, try again'%>
<% end %>
信息如下所示:
[{"id"=>1, "name"=>"Bitcoin", "symbol"=>"BTC", "slug"=>"bitcoin", "num_market_pairs"=>9713, "date_added"=>"2013-04-28T00:00:00.000Z", "tags"=>["mineable", "pow", "sha-256", "store-of-value", "state-channels", "coinbase-ventures-portfolio", "three-arrows-capital-portfolio", "polychain-capital-portfolio"], "max_supply"=>21000000, "circulating_supply"=>18633612, "total_supply"=>18633612, "platform"=>nil, "cmc_rank"=>1, "last_updated"=>"2021-02-20T07:45:02.000Z", "quote"=>{"USD"=>{"price"=>55541.51774356704, ...
我不确定如何以正确的方式做到这一点
您似乎无法匹配实际的 symbol
值,而是希望 @symbol
与哈希完美匹配,但事实并非如此。
其次,您还每次通过循环打印“忘记写东西”消息,这是错误的。您可以摆脱循环,只需使用 find
立即定位条目,作为奖励,您可以更轻松地检测到“遗漏”条件。
看看是否能找到匹配的条目,否则显示消息。
修复看起来像:
<%- if @symbol.blank? -%>
Sorry, but you forgot to write something.
<%- else -%>
<%- found = @lookup_coins.find { |c| c['symbol'] == @symbol } -%>
<%- if (found) -%>
<%= found["symbol"]%> <br/> <%= found["name"]%>:
<%= number_to_currency(found['quote']['USD']['price'].round(2), :unit => "$ ") %>
<br/>
<%= found["cmc_rank"]%> <br/>
<%- else -%>
Sorry, there was a mistake, try again
<%- end -%>
<%- end -%>
在这里使用 @symbol.blank?
比 == ''
更宽容,因为它也会拒绝一堆空格 and/or 制表符。
提示:不要使用 <%= '...' %>
,而是按原样将文本放在那里。默认是回显它。
<% if @symbol %>
<%@resultant_symbol = @lookup_coins['data'].select {|lookup_coin| lookup_coin["symbol"] == @symbol }%>
<%if @resultant_symbol.present?%>
<% x = @resultant_symbol.first %>
<%= x["symbol"]%> <br/> <%= x["name"]%>: <%= number_to_currency(x['quote']['USD']['price'].round(2) , :unit => "$ ") %> <br/> <%= x["cmc_rank"]%> <br/>
<%else%>
<%= @symbol = 'Sorry, there was a mistake, try again'%>
<%end%>
我有这个代码:
<% @lookup_coins.each do |x| %>
<% if @symbol == '' %>
<%= 'Sorry, but you forgot to write something, LOL'%>
<% elsif @symbol == x %>
<%= x["symbol"]%> <br/> <%= x["name"]%>: <%= number_to_currency(x['quote']['USD']['price'].round(2) , :unit => "$ ") %> <br/> <%= x["cmc_rank"]%> <br/>
<% else 'Sorry, there was a mistake, try again'%>
<% end %>
信息如下所示:
[{"id"=>1, "name"=>"Bitcoin", "symbol"=>"BTC", "slug"=>"bitcoin", "num_market_pairs"=>9713, "date_added"=>"2013-04-28T00:00:00.000Z", "tags"=>["mineable", "pow", "sha-256", "store-of-value", "state-channels", "coinbase-ventures-portfolio", "three-arrows-capital-portfolio", "polychain-capital-portfolio"], "max_supply"=>21000000, "circulating_supply"=>18633612, "total_supply"=>18633612, "platform"=>nil, "cmc_rank"=>1, "last_updated"=>"2021-02-20T07:45:02.000Z", "quote"=>{"USD"=>{"price"=>55541.51774356704, ...
我不确定如何以正确的方式做到这一点
您似乎无法匹配实际的 symbol
值,而是希望 @symbol
与哈希完美匹配,但事实并非如此。
其次,您还每次通过循环打印“忘记写东西”消息,这是错误的。您可以摆脱循环,只需使用 find
立即定位条目,作为奖励,您可以更轻松地检测到“遗漏”条件。
看看是否能找到匹配的条目,否则显示消息。
修复看起来像:
<%- if @symbol.blank? -%>
Sorry, but you forgot to write something.
<%- else -%>
<%- found = @lookup_coins.find { |c| c['symbol'] == @symbol } -%>
<%- if (found) -%>
<%= found["symbol"]%> <br/> <%= found["name"]%>:
<%= number_to_currency(found['quote']['USD']['price'].round(2), :unit => "$ ") %>
<br/>
<%= found["cmc_rank"]%> <br/>
<%- else -%>
Sorry, there was a mistake, try again
<%- end -%>
<%- end -%>
在这里使用 @symbol.blank?
比 == ''
更宽容,因为它也会拒绝一堆空格 and/or 制表符。
提示:不要使用 <%= '...' %>
,而是按原样将文本放在那里。默认是回显它。
<% if @symbol %>
<%@resultant_symbol = @lookup_coins['data'].select {|lookup_coin| lookup_coin["symbol"] == @symbol }%>
<%if @resultant_symbol.present?%>
<% x = @resultant_symbol.first %>
<%= x["symbol"]%> <br/> <%= x["name"]%>: <%= number_to_currency(x['quote']['USD']['price'].round(2) , :unit => "$ ") %> <br/> <%= x["cmc_rank"]%> <br/>
<%else%>
<%= @symbol = 'Sorry, there was a mistake, try again'%>
<%end%>