我正在使用一个API,当没有返回任何内容时,它会为nilClass
"提供一个" no方法。错误。所以我添加了一堆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
end
money.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>
答案 0 :(得分:1)
我想我在这一行看到了你的语法错误:
<p><%= number_to_currency(@pacs_money + " from " + @pacs + " PACs" %></p>
number_to_currency
没有右括号。它应该是:
<p><%= number_to_currency(@pacs_money) + " from " + @pacs + " PACs" %></p>