前提・実現したいこ
railsでのアプリケーション制作をしています。
ネットの情報を調べscaffoldを使って制作し、あっさり実装することが出来ましたが
自身で制作中のアプリはscaffoldを使っていません。
下記内容です。
投稿記事一覧ページに
<検索機能>を実装したい。
モデル blos カラム名title
内にあるものを検索した後、
該当するものが一覧で表示されるようにしたいです。
発生している問題・エラーメッセージ
エラーメッセージは表示されていないのですが、
検索後、画面表示は変わらない状態です。
該当のソースコード
<コントローラー>
```ここに言語を入力 class BlogsController < ApplicationController def index @blog = Blog.new @blogs = Blog.search(params[:search]) end def show @blog = Blog.find(params[:id]) end def new @blog = Blog.new end def create blog = Blog.new(blog_params) blog.save redirect_to blog_path(blog.id) end def edit @blog = Blog.find(params[:id]) end def update blog = Blog.find(params[:id]) blog.update(blog_params) redirect_to blog_path(blog.id) end def destroy blog = Blog.find(params[:id]) blog.destroy redirect_to blogs_path end private def blog_params params.require(:blog).permit(:title, :opinion, :image) end end
<ビュー>
<h1>books</h1> <%= form_tag("/blogs",method:"get") do %> <input type="text" title="search"> <input type="submit" value="search"> <% end %> <table> <thead> <tr> <th>title</th> <th>opinion</th> <th>image</th> </tr> </thead> <tbody> <% @blogs.each do |blog| %> <tr> <td> <%= blog.title %> </td> <td> <%= blog.opinion %> </td> <td> <%= attachment_image_tag blog, :image, :fill, 30, 30, format: 'jpeg' %> <td> <%= link_to "show", blog_path(blog.id) %> <%= link_to "edit", edit_blog_path(blog.id) %> <%= link_to "destroy", blog_path(blog.id), method: :delete %> </td> </tr> </tbody> <% end %> </table> <h2>new book</h2> <%= form_for(@blog) do |f| %> <h4>title</h4> <%= f.text_field :title %> <h4>opinion</h4> <%= f.text_field :opinion %> <h4>image</h4> <%= f.attachment_field :image %> <br> <%= f.submit 'create' %> <% end %> <footer> <h3></h3> </footer>
<モデル>
```ここに言語を入力 class Blog < ApplicationRecord attachment :image def self.search(search) if search Blog.where(Blog.arel_table[:title].matches("%#{search}%")) else Blog.all end end end
初めての質問で言葉足らずで申し訳ありません。 よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。