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



我是完整堆栈开发的新手,我正在尝试获得一个简单的搜索表格。该网页应该将用户重定向到包含关键字的所有视频的列表。每当我键入存在的标题时,我都会得到:nomethoderror at/posts/:title/search

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

这是我的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>

尝试更改

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

to

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

获得不需要完全匹配的答案(即匹配案例等(

同样在您的搜索表格中,您有两个 type 属性。您应该将其中一个更改为

name="title"

最新更新