Ruby on rails 搜索结果不打印?

Ruby on rails 搜索结果不打印?,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我在application.html.erb标题中构建了一个搜索栏,允许访问者在我的网站上搜索“用户”,单击“提交”按钮显示搜索结果时,找到的结果似乎不会打印在我的页面上(只是空白) 我已经在我的users文件夹下创建了index.html.erb以及_user.html.erb,所以我不知道为什么会发生这种情况(据我所知,_user.html.erb是显示查找/渲染结果所必需的) 是否有一行代码应该放在_user.html.erb中 下面是我的index.html.erb中的内容: <%

我在application.html.erb标题中构建了一个搜索栏,允许访问者在我的网站上搜索“用户”,单击“提交”按钮显示搜索结果时,找到的结果似乎不会打印在我的页面上(只是空白)

我已经在我的users文件夹下创建了index.html.erb以及_user.html.erb,所以我不知道为什么会发生这种情况(据我所知,_user.html.erb是显示查找/渲染结果所必需的)

是否有一行代码应该放在_user.html.erb中

下面是我的index.html.erb中的内容:

<% if @user.present? %>
  <%= render @user %>
<% else %>
  <p>There are no posts containing the term(s) <%= params[:search] %>.</p>
<% end %>
user.rb

def index
  @user = User.all
  if params[:search]
    @user = User.search(params[:search]).order("created_at DESC")
  else
    @user = User.all.order('created_at DESC')
  end
end
def self.search(search)
  where("firstname LIKE ?", "%#{search}%") 
  where("lastname LIKE ?", "%#{search}%")
end
<%= user.firstname %> <%= user.lastname%>

您在
搜索
方法上没有得到任何结果,这就是为什么您在搜索之后没有看到任何结果。更改您的
where
语句,因为它正在创建和查询,并且您可能正在查找OR。请尝试以下方式:

def self.search(search)
    where("firstname LIKE ? OR lastname LIKE ?", "%#{search}%", "%#{search}%")
end
希望这有助于查看 通常不会将
params
传递回视图-我认为主要是因为它会随着每个请求而改变,最终可能会与您期望的有所不同。您可能希望将其复制到控制器中的实例变量

<% if @users.present? %>
  <%= render @users %>
<% else %>
  <p>There are no posts containing the term(s) <%= @search %>.</p>
<% end %>
模型
where(“firstname LIKE?”,“%#{search}%”
被忽略。它运行它,但丢弃结果,只返回下一行的结果。你想要:

def self.search(search)
  where("firstname LIKE :p OR lastname LIKE :p", p: "%#{search}%")
end

多字段搜索 -在多个字段中搜索单个参数中的多个标记

如果你想试试这个,你就是受虐狂。但这是可能的,我以前也做过

注意:这是为SQL Server 2008编写的,因为我的公司当时正在使用SQL Server 2008。一些SQL函数——特别是
IsNull
之类的
——可能会根据您使用的内容而有所不同

类用户
范围:搜索,->(值)do
#这允许令牌包含A-z、0-9、和%。还有别的吗
#作为分隔符。
多字段搜索(%w[firstname-lastname],value.split(/[^\w%]/)
结束
#此范围是通用的,并且可用于中字符串字段的任何组合
#任何型号。
范围:多字段搜索,->(字段、值)do
#你在看这些领域。。。
fields|子句=fields.select(&:present?).map do| field|
“IsNull(#{field},”)”
end.join(“+”+”)
# ... 对于这些值。。。
values_子句=清理_条件(values.select(&:present?).map do| value|
“combo_view.composition,如“%#{value}%””
end.join('AND'))
# ... 通过构造一个查询将字段连接在一起,然后
#分别搜索搜索参数的每个标记。

加入(你能不能加入
\u user.html.erb
?是的,你必须在你的
\u user.html.erb
中添加类似的内容,
,因为@user是用户模型的一个实例。更改为你的建议,但仍然没有打印结果:/这很有效-谢谢!如果我在搜索字段中键入名字和姓氏怎么办例如,搜索“Brittany”会得到一个名和姓的结果,但搜索“Brittany Miller”没有结果。哦,不久前我还玩得很开心。很糟糕。我会在上面添加一个更新。非常感谢-奇怪的是,搜索多个字段似乎是一项简单/常见的任务?!不?我的rails应用程序位于Sqlite3上。
def self.search(search)
  where("firstname LIKE :p OR lastname LIKE :p", p: "%#{search}%")
end
class User
  scope :search, ->(value) do
    # This allows tokens containing A-z, 0-9, _, and %. Anything else is treated
    # as a delimiter.
    multi_field_search(%w[firstname lastname], value.split(/[^\w%]/))
  end

  # This scope is generic, and reusable for any combination of string fields in
  # any model.
  scope :multi_field_search, ->(fields, values) do
    # You're looking in these fields...
    fields_clause = fields.select(&:present?).map do |field|
      "IsNull(#{field}, '')"
    end.join(" + ' ' + ")

    # ... for these values...
    values_clause = sanitize_conditions(values.select(&:present?).map do |value|
      "combo_view.combination LIKE '%#{value}%'"
    end.join(' AND '))

    # ... and you do it by constructing a query to join the fields together, and
    # searching for each token of the search parameter individually.
    joins(<<-SQL).where(values_clause)
      INNER JOIN (
        SELECT *, #{fields_clause} AS combination
        FROM #{table_name}
      ) AS combo_view ON #{table_name}.#{primary_key} = combo_view.#{primary_key}
    SQL
  end

  # ...
end