我正在打开一个API,当没有返回时,它抛出了一个“NoMethodfornilClass”错误。因此,我添加了一组if/else语句来处理这个问题。下面是我的密码。
现在我收到了一个错误:
syntax error, unexpected keyword_end, expecting ')' end因为它起作用了,我觉得我的路线很好。我在下面的视图中标记了导致错误的行。当我删除那个end时,它仍然会抛出错误:
main_controller.rb
def money
@first = params[:first_name]
@last = params[:last_name]
@politician = JSON.load(open("http://..."))
if @politician.empty?
@politician = "Enter a politician's name."
else
@year = params[:year]
@pol_id = @politician[0]["id"]
@breakdown = JSON.load(open("http://..."))
if (@breakdown["Individuals"]).nil?
@individuals_money = 0
@individuals = 0
else
@individuals_money = (@breakdown["Individuals"][1]).to_f
@individuals = @breakdown["Individuals"][0]
end
if (@breakdown["PACs"]).nil?
@pacs_money = 0
@pacs = 0
else
@pacs_money = (@breakdown["PACs"][1]).to_f
@pacs = @breakdown["PACs"][0]
end
@total_money = @individuals_money + @pacs_money
@top_contributors = JSON.load(open("http://..."))
end
endmoney.html.erb
<div class = "col-md-6">
<% if @politician == "Enter a politician's name." %>
<br />
<p><%= @politician %></p>
<% else %>
<h3><%= @politician[0]["name"] + " for " + @year %></h3>
<p><%= number_to_currency(@total_money) + " Total received"%></p>
<p><%= number_to_currency(@individuals_money) + " from " + number_with_delimiter(@individuals, :delimiter => ',') + " contributors" %></p>
<p><%= number_to_currency(@pacs_money + " from " + @pacs + " PACs" %></p>
<h4>Top Contributors</h4>
<% y = 0 %>
<% for y in 0..9 %>
<p><%= (y+1).to_s + ". " + @top_contributors[y]["name"].to_s + " " + number_to_currency(@top_contributors[y]["total_amount"].to_i) %></p>
<% end %>
<% end %> <<<<==== ***This is the line throwing the error***
</div>发布于 2014-07-16 19:41:41
我想我看到了这行的语法错误:<p><%= number_to_currency(@pacs_money + " from " + @pacs + " PACs" %></p>
对于number_to_currency,没有结束括号。它应该是:
<p><%= number_to_currency(@pacs_money) + " from " + @pacs + " PACs" %></p>
https://stackoverflow.com/questions/24788969
复制相似问题