如何使用Sinatra和Datamapper搜索所有包含关键字的帖子?

问题描述 投票:0回答:1

我对全栈开发相当陌生,我正在尝试获取一个简单的搜索表单来工作。该网页应该将用户重定向到包含所有包含关键字的视频的列表的页面。每当输入存在的标题时,都会在/ posts /:title / search

:NoMethodError]中出现

我尝试使用查询,但是失败。

这是我的app.rb。我查询所有包含:title的视频。

get "/posts/:title/search" do
    # authenticate!
    @results = Video.all(title: params[:title])

    if @results
        erb :search_success
    else
        flash[:error] = "Video not found."
        erb :search_success
    end

end

这是search_success.erb,我要在其中包含标题中包含关键字的视频的列表。

<div class="container">
    <% @results.each do |r| %>
        <h1><%= r.title %></h1>
    <% end %>
</div>

这是搜索表单所在的navigation.erb。

<form action="/posts/:title/search" method="get">
        <input type="text" type="text" placeholder="Search" aria-label="Search">
        <button type="submit">Search</button>
  </form>
html ruby forms sinatra datamapper
1个回答
0
投票

尝试更改

@results = Video.all(title: params[:title])

to

@results = Video.all(:title.like => "%#{params[:title])}%")

获得不需要完全匹配的答案(例如,区分大小写等)

也在搜索表单中,您具有两个type属性。您应该将其中之一更改为

name="title"
© www.soinside.com 2019 - 2024. All rights reserved.