[英]undefined method nil:NilClass ruby
What I need to do is create a page where the user will type in a last name and the system will return information related to it. 我需要做的是创建一个页面,用户将在其中输入姓氏,系统将返回与其相关的信息。 I keep receiving the error "undefined method `each' for nil:NilClass."
我不断收到错误“ nil:NilClass的未定义方法'each'”。 I am stuck and can not debug it any help or guidance would be greatly appreciated.
我陷入困境,无法调试它,将不胜感激任何帮助或指导。
CODE FOR THE INPUT PAGE 输入页面代码
<%= form_tag(showcustcourses_custcoursesout_path, :controller => "showcustcourses", :action => "custcoursesout", :method => "post") do %>
<div class="field">
<%= label_tag :Customer_Name %><br />
<%= text_field_tag :customer_name_in %>
</div>
<div class="actions">
<%= submit_tag "Submit Customer Name" %>
</div>
<% end %>
CODE FOR THE CONTROLLER 控制器代码
class BookinController < ApplicationController
def bookin
end
def bookout
@customer_name = params[:customer_name_in]
@r = Customer.find_by_last(@customer_name)
@rate_list = @r ? @r.rates : nil
end
end
CODE FOR THE OUTPUT PAGE ( <% @customer_list.each do |m| %>
is throwing the error) 输出页面的代码(
<% @customer_list.each do |m| %>
引发错误)
<h1>Bookin#bookout</h1>
<p>Find me in app/views/bookin/bookout.html.erb</p>
<center><table width = 65% border = 1>
<tr> <th> Customer Name</th><th> Room Number </th> <th> Cost </th></tr>
<% @customer_list.each do |m| %>
<tr> <td> <%= m.name %> </td> <td> <%= m.roomnumber %> </td> <td> <%= m.cost %> </td> </tr>
<% end %>
</table> </center><br /> <br />
You are getting the error undefined method 'each' for nil:NilClass
because you forgot to set the value of instance variable @customer_list
. 您正在得到
undefined method 'each' for nil:NilClass
的错误undefined method 'each' for nil:NilClass
因为您忘记设置实例变量@customer_list
的值。 So, @customer_list
is nil
. 因此,
@customer_list
为nil
。
You need to set @customer_list
variable in the action corresponding to your view which in your case is bookout
action as you are rendering bookout.html.erb
. 您需要在与您的视图相对应的操作中设置
@customer_list
变量,这种情况下,您在呈现bookout.html.erb
是bookout
操作。
Simply, do this in BookinController#bookout
: 只需在
BookinController#bookout
执行以下BookinController#bookout
:
def bookout
## ...
@customer_list = Customer.all ## Add this
end
UPDATE 更新
As per the chat discussion, OP needed to show last(from customers table), roomlevel(from apples table), cost(from rates table) 根据聊天讨论,OP需要显示last(来自customers表),roomlevel(来自apples表),cost(来自rates表)
Suggested to modify 建议修改
bookout
method as below: bookout
方法如下:
def bookout
@customer_list = Customer.all
@customer_name = params[:customer_name_in]
end
and bookout.html.erb
as below: 和
bookout.html.erb
如下:
<% @customer_list.each do |customer| %>
<% customer.bookings.each do |booking| %>
<tr>
<td> <%= customer.last %> </td>
<td> <%= booking.apple.room_level %> </td>
<td> <%= booking.apple.cost %> </td>
</tr>
<% end %>
<% end %>
Also, OP's schema was not correct to achieve this result. 此外,OP的架构也不适合实现此结果。 Added
apple_id
to bookings
table and removed rate_id
from it. 在
bookings
表中添加了apple_id
,并从其中删除了rate_id
。
NOTE: As you don't want bookings to be associated with rates
table, rate_id
was removed from bookings
table. 注意:由于您不希望预订与
rates
表相关联,因此rate_id
从bookings
表中删除。 You would have to add cost
field in apples
table to display the cost in the view. 您必须在
apples
表中添加cost
字段才能在视图中显示成本。
Add this in your controller. 将此添加到您的控制器中。 It will bring details of all customers.
它将带来所有客户的详细信息。
def bookin
@customer_list = Customer.all
end
def bookout
@customer_list = Customer.all
@customer_name = params[:customer_name_in]
@r = Customer.find_by_last(@customer_name)
@rate_list = @r ? @r.rates : nil
end
If still it returns nil error, then it means you do not have any customer records in database. 如果仍然返回nil错误,则意味着您在数据库中没有任何客户记录。
<h1>Bookin#bookout</h1>
<p>Find me in app/views/bookin/bookout.html.erb</p>
<center><table width = 65% border = 1>
<tr> <th> Customer Name</th><th> Room Number </th> <th> Cost </th></tr>
#check if object is not nil
<% if !@customer_list.nil? %>
<% @customer_list.each do |m| %>
<tr>
<td> <%= m.name %> </td>
<td> <%= m.roomnumber %> </td>
<td> <%= m.cost %>
</td>
</tr>
<% end %>
<% else %>
<p> no customers available </p>
<% end %>
</table>
</center>
@customer_list is not defined 未定义@customer_list
defined it in controller like this 像这样在控制器中定义它
@customer_list = Customer.all @customer_list = Customer.all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.