无法在 Rails App 中使用基本的 searchlogic 插件代码
我的 Rails 应用程序抛出此错误:
将 id 称为 nil,这会被错误地设置为 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>
我相信问题是由于搜索未初始化,但我不确定如何做到这一点。
My rails app is throwing this error:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
I am trying to make a basic form that will allow a user to search for a "match" by "country". It is just a proof of concept as I am still learning.
Here is my model:
class OmMatch < ActiveRecord::Base
end
Here is my controller:
class OmMatchesController < ApplicationController
def search
@search = OmMatch.search(params[:search])
@match = @search.all
end
end
Here is the view:
<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>
I believe that the problem is from search not being initialized but I am not sure how to that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您这样做时,
它期望@search成为一个初始化的活动记录对象,其路由在routes.rb文件中定义。
我假设您遇到的问题是在对 OmMatch 搜索操作执行 GET 请求时。
如果搜索是一个可以初始化的对象,那么只需添加
到您的控制器即可。
您需要考虑这里的工作流程,即通过链接请求表单时运行什么操作,以及用户发布表单时会发生什么。
请记住,您可以运行
来查看应用程序了解的资源和控制器的所有路由。
我建议您查看 Railscasts 屏幕截图,尤其是有关路由的屏幕截图。了解控制器代码如何通过 url 进行映射非常重要。 http://railscasts.com/episodes?search=routing 正确映射路线后并且您了解何时调用每个操作,您可以确保在需要对象之前创建对象。
When you do
It is expecting @search to be an initialized active record object with routes defined in the routes.rb file.
I assume the problem you are having is when doing a GET request to the OmMatch search action.
If search is an object you can initialize, then just add
to your controller.
You need to consider the workflow here for what action is run when the form is requested via a link, and what occurs when a user posts the form.
Remember that you can run
to see all the routes that your application knows about for your resources and controllers.
I would recommend that you check out the railscasts screen casts, especially the ones on routing. It is very important that you understand how your controller code is mapped by urls. http://railscasts.com/episodes?search=routing Once you have your routes mapped correctly and you understand when each action will be invoked, you can ensure that your objects are created before you need them.