検索機能が実装できない(Ruby on Rails)

前提

プログラミング初心者です。
簡単なto do リストを作っています。
追加実装として、検索機能を取り入れたところ、エラーメッセージが発生しました。

https://gyazo.com/e681570f1062b2e846ed93b717607219

実現したいこと

検索するボタンを押すと、該当するタスクが表記できるようにしたい

発生している問題・エラーメッセージ

TasksController#search is missing a template for request formats: text/html NOTE! Unless told otherwise, Rails expects an action to render a template with the same name, contained in a folder named after its controller. If this controller is an API responding with 204 (No Content), which does not require a template, then this error will occur when trying to access it via browser, since we expect an HTML template to be rendered for such requests. If that's the case, carry on.

該当のソースコード

app/views/tasks/search.html.erb

<%= form_with(url: search_tasks_path, local: true, method: :get, class: "search-form") do |form| %> <%= form.text_field :keyword, placeholder: "投稿を検索する", class: "search-input" %> <%= form.submit "検索", class: "search-btn" %> <% end %> <div class="contents row"> <% @tasks.each do |task| %> <%= render partial: "task", locals: { task: task } %> <% end %> </div>

config/route.rb

Rails.application.routes.draw do devise_for :users root to: "tasks#index" get 'tasks', to: 'tasks#index' get 'tasks/new', to: 'tasks#new' post 'tasks/create', to: 'tasks#create' delete "tasks/:id", to: 'tasks#destroy' resources :tasks do collection do get 'search' end end end

app/controllers/tasks_controller

class TasksController < ApplicationController def index @tasks = Task.all end def new end def create Task.create(title:params[:title]) redirect_to "/tasks" end def destroy @task = Task.find(params[:id]) @task.destroy redirect_to "/tasks" end def search @tasks = Task.search(params[:keyword]) end end

app/models/task.rb

class Task < ApplicationRecord def self.search(search) if search != "" Task.where('title LIKE(?)', "%#{search}%") else Task.all end end end

試したこと

app/views/tasks/search.html.erbのテンプレート内容の確認

コメントを投稿

0 コメント