Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActionView::Template::Error(对于nil:NilClass,未定义方法`currency';):_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails ActionView::Template::Error(对于nil:NilClass,未定义方法`currency';):

Ruby on rails ActionView::Template::Error(对于nil:NilClass,未定义方法`currency';):,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一段代码,看起来像这样 <% @current_user.prices.each_with_index do | t, i| %> <tr> <td> <%= (i+1) %> </td> <td> <%= t.country %> </td> <td> <%= t.network %> </td>

我有一段代码,看起来像这样

<% @current_user.prices.each_with_index do | t, i| %>
    <tr>
        <td> <%= (i+1) %> </td> 
        <td> <%= t.country %> </td>
        <td> <%= t.network %> </td>
        <td> <%= t.send("price_#{@current_user.currency.downcase}") %> </td>
    </tr>       
<% end %>

但它给出了错误

ActionView::Template::Error (undefined method `currency' for nil:NilClass):
    30:                             <td> <%= (i+1) %> </td> 
    31:                             <td> <%= t.country %> </td>
    32:                             <td> <%= t.network %> </td>
    33:                             <td> <%= t.send("price_#{@current_user.currency.downcase}")  %> </td>
    34:                         </tr>       
    35:                     <% end %>
    36:                 <% else %>
  app/views/prices/index.html.erb:33:in `block in _app_views_prices_index_html_erb__1511544251155426207_16153880'
  app/views/prices/index.html.erb:28:in `_app_views_prices_index_html_erb__1511544251155426207_16153880'
ActionView::Template::Error(nil:NilClass的未定义方法“currency”):
30:                                
31:                               
32:                               
33:                               
34:                                
35:                     
36:                 
app/views/prices/index.html.erb:33:in'block-in\u-app\u-views\u-prices\u-index\u-html\u-erb\u 1511544251155426207\u 16153880'
app/views/prices/index.html.erb:28:in`_app_views_prices_index_html_erb_1511544251155426207_16153880'
然而,在一个非常相似的情况下,它工作得很好

<% @current_user.prices.each_with_index do | t, i| %>
    <tr>
       <td> <%= (i+1) %> </td> 
       <td> <%= t.country %> </td>
       <td> <%= t.network %> </td>
       <td> <%= t.send("price_#{@current_user.currency.downcase}") %> </td>
   </tr>    


为什么它在一个场景中有效,而在另一个场景中无效?可能我只是累了。

对于一个用户,它有一些值,而对于当前用户,货币值是零,这就是为什么会出现此错误。您可以使用控制台或应用断点检查当前用户货币值。

您的
@current\u user
变量似乎是零,您正在为nil对象调用货币

解决方案

如果您只想跳过此错误,那么可以使用下面的代码那样的
try()

例如:

@current_user.try(:currency).try(:downcase)
# try() behaves something like ternary operator (if then else)

但是,我建议您为
@current_user
分配所需的值。因为如果使用
try()
,如果父对象为nil,它将返回nil。因此,调试
@current\u user
变量并为其分配正确的值。

@current\u user.currency.downcase
不起作用,因为
索引控制器
未通过designe验证,因此未定义
@current\u user


但是,我无法解释为什么在这一行的上面几行定义它。

问题的顶部和底部有两段完全相同的代码。原因是什么?在这种情况下,您的
@current_用户
nil
。很可能您忘记在控制器中分配它。是的,我看不出两者之间的区别,但是被遗忘的
应该会导致语法错误。如果在
.current\u user上调用
.currency
时@current\u user是nil,那就有点奇怪了,因为在
上调用
时它会是nil。你确定你已经复制并粘贴了你的代码?如果您在这里输入了它,您可能没有在代码中重复输入错误。或者,在您进行的另一个方法调用中,可能有东西正在重置@current_user(为nil)。