无法在 Rails App 中使用基本的 searchlogic 插件代码

发布于 2024-08-20 04:19:52 字数 1238 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2024-08-27 04:19:52

当您这样做时,

<% form_for @search do |f| %>

它期望@search成为一个初始化的活动记录对象,其路由在routes.rb文件中定义。

我假设您遇到的问题是在对 OmMatch 搜索操作执行 GET 请求时。

如果搜索是一个可以初始化的对象,那么只需添加

@search = Search.new

到您的控制器即可。

您需要考虑这里的工作流程,即通过链接请求表单时运行什么操作,以及用户发布表单时会发生什么。

请记住,您可以运行

rake routes

来查看应用程序了解的资源和控制器的所有路由。

我建议您查看 Railscasts 屏幕截图,尤其是有关路由的屏幕截图。了解控制器代码如何通过 url 进行映射非常重要。 http://railscasts.com/episodes?search=routing 正确映射路线后并且您了解何时调用每个操作,您可以确保在需要对象之前创建对象。

When you do

<% form_for @search do |f| %>

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

@search = Search.new

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

rake routes

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文