How to search for all posts containing keyword using Sinatra and Datamapper

您所在的位置:网站首页 datamapper How to search for all posts containing keyword using Sinatra and Datamapper

How to search for all posts containing keyword using Sinatra and Datamapper

2023-02-21 07:44| 来源: 网络整理| 查看: 265

I am fairly new to full stack development and I am trying to get a simple search form to work. The webpage is supposed to redirect the user to a page with a list of all videos containing the keyword. Whenever I type in a title that exists, I get :NoMethodError at /posts/:title/search

I've tried using a query but have failed.

This is my app.rb. I query all videos that contain :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

This is search_success.erb where I want to have a list of the videos that contains the keyword in the title.

This is navigation.erb where the search form lives.

Search

Try changing

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

to

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

to get answers that don't need to match completely (i.e. match case etc.)

Also in your search form, you have two type attributes. You should change one of them to

name="title" Credits by (MrMos),This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.Source: Stack Overflow


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3