我的 Rails 应用程序抛出此错误:
为 nil 调用 id,它会错误地为 4 - 如果你真的想要 nil 的 id,请使用 object_id
我正在尝试制作一个基本表单,允许用户按“国家”搜索“匹配”。这只是一个概念证明,因为我仍在学习。
这是我的模型:
class OmMatch < ActiveRecord::Base
end
这是我的控制器:
class OmMatchesController < ApplicationController
def search
@search = OmMatch.search(params[:search])
@match = @search.all
end
end
这是视图:
<html>
<head><title>"Matches"</title></head>
<body>
<% form_for @search do |f| %>
<p>
<%= f.label :country_equals, "Country" %><br />
<%= f.text_field :country_equals %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
<table>
<tr>
<th>"Match Name"</th>
<th>"Country"</th>
</tr>
<% @match.each do |match| %>
<tr>
<td><%=h match.matchname %></td>
<td><%=h match.country %></td>
</tr>
<% end %>
<table>
</body>
</html>
我相信问题出在搜索未初始化,但我不知道该怎么做。