localhost:3000 中的 NoMethodError
NoMethodError in localhost:3000
我一直在使用 rails 服务器来预览我对在 gh 上共享给我的网站代码所做的一些 html/css 更改,但我无法访问某些页面,因为出现此错误:
/media 处没有方法错误
nil:NilClas
的未定义方法“each”
这是用户个人资料中功能的页面;我在终端中创建了一个假用户,这样我就可以访问个人资料页面,但是我想要的个人资料页面却出现了那个错误。
这是代码swowing的部分:
<div class="select_wrapper">
<select name="trienal_id" id="" required>
<option value="" disabled>Trienal</option>
**<% Subject.where(discipline_type: 0)[1..3].each do |s| %>**
<option value="<%= s.id %>" <%= "selected" if @tri_id==s.id %>><%= s.name %></option>
<% end %>
</select>
</div>
Subject.where
返回的记录数可能少于您的范围索引指定的记录数。不知道为什么你有 [1..3]
在那里。在 Subject.where 上使用 each,如果需要根据条件对其进行更多限制,请将其添加到 where 子句中。
<% Subject.where(discipline_type: 0).each do |s| %>
<!-- your code here -->
<% end %>
在 irb 中试试这个 ($ irb)
> x = []
> x[0]
=> nil
> x[1]
=> nil
所以很可能您的查询 Subject.where(discipline_type: 0)
返回 blank array []
后跟 [1..3]
结果是 nil
后跟 each
这将导致 undefined method each for nil:NilClas
您应该使用 limit 函数(它指定要检索的记录数的限制)
例如
Subject.where(discipline_type: 0).limit(3)
因此您的代码应该如下所示
<% Subject.where(discipline_type: 0).limit(3).each do |s| %>
<option value="<%= s.id %>" <%= "selected" if @tri_id==s.id %>><%= s.name %></option>
<% end %>
我一直在使用 rails 服务器来预览我对在 gh 上共享给我的网站代码所做的一些 html/css 更改,但我无法访问某些页面,因为出现此错误:
/media 处没有方法错误 nil:NilClas
的未定义方法“each”这是用户个人资料中功能的页面;我在终端中创建了一个假用户,这样我就可以访问个人资料页面,但是我想要的个人资料页面却出现了那个错误。
这是代码swowing的部分:
<div class="select_wrapper">
<select name="trienal_id" id="" required>
<option value="" disabled>Trienal</option>
**<% Subject.where(discipline_type: 0)[1..3].each do |s| %>**
<option value="<%= s.id %>" <%= "selected" if @tri_id==s.id %>><%= s.name %></option>
<% end %>
</select>
</div>
Subject.where
返回的记录数可能少于您的范围索引指定的记录数。不知道为什么你有 [1..3]
在那里。在 Subject.where 上使用 each,如果需要根据条件对其进行更多限制,请将其添加到 where 子句中。
<% Subject.where(discipline_type: 0).each do |s| %>
<!-- your code here -->
<% end %>
在 irb 中试试这个 ($ irb)
> x = []
> x[0]
=> nil
> x[1]
=> nil
所以很可能您的查询 Subject.where(discipline_type: 0)
返回 blank array []
后跟 [1..3]
结果是 nil
后跟 each
这将导致 undefined method each for nil:NilClas
您应该使用 limit 函数(它指定要检索的记录数的限制) 例如
Subject.where(discipline_type: 0).limit(3)
因此您的代码应该如下所示
<% Subject.where(discipline_type: 0).limit(3).each do |s| %>
<option value="<%= s.id %>" <%= "selected" if @tri_id==s.id %>><%= s.name %></option>
<% end %>