gpt4 book ai didi

ruby-on-rails - 在 Rails 中搜索

转载 作者:行者123 更新时间:2023-12-03 13:39:30 25 4
gpt4 key购买 nike

我有一个“帖子”表,其属性包括 titlebody .

post_controller.rb:

class PostsController < ApplicationController
def index
@posts = Post.search(params[:search], params[:id])
end
end

index.html.erb:
 <%= form_tag posts_path, :method => 'get'  do %>
<%= text_field_tag :search, params[:search]%>
<%= submit_tag "Search", :name => nil %>
<% end %>

<hr />
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<tr>
<td><hr></td>
<td><hr></td>
</tr>
<% @posts.each do |post| %>

<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', :action => :show, :id => post.id %></td>
<td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
<td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %></td>
</tr>
<tr>
<td><hr></td>
<td><hr></td>
</tr>
<% end %>
</table>

和 post.rb
def self.search(search, id)
if search
where(['name LIKE ?', "%#{search}%"])
else
scoped
end
end

当我提交搜索参数时,我收到一条错误消息:
ActiveRecord::StatementInvalid in Posts#index 
SQLite3::SQLException: no such column: name: SELECT "posts".* FROM "posts" WHERE (name LIKE '%lorem%')

Extracted source (around line #23):

23: <% @posts.each do |post| %>

APD:我想按“标题”搜索。

最佳答案

虽然这不是您问题的直接答案,但这里有一些资源可以帮助您了解搜索并在 Rails 应用程序中实现它。

A Simple search form

Advanced search form

Searching with AJAX

Powerful search functionality with the Sunspot gem

List of the most popular search tools for Ruby

- - - - - - - -更新 - - - - - - - -

Elasticsearch 之所以越来越受欢迎,是因为它具有一些现代的功能,例如即时索引。它有一颗名为轮胎的 ruby 。绝对值得一看。

Elasticsearch

Tire

---------------更新 2----------------

轮胎已退役,已被 Elasticsearch-ruby 取代

关于ruby-on-rails - 在 Rails 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12010780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com