railsで2つのモデルから検索機能を使って表示させようと思い、Userモデル、完全一致でMH(name)と検索すると下記エラーが出てしまいます。これは入力したMHが認識されていないということでしょうか。
どなたかアドバイス頂けると助かります。
ActiveRecord::StatementInvalid in Searches#search
Showing /home/vagrant/work/bookers-app_5/app/views/searches/search.html.erb where line #19 raised:
SQLite3::SQLException: no such column: MH: SELECT "users".* FROM "users" WHERE (MH)
Extracted source (around line #19):
17
18
19
20
21
22
<tbody> <% @user.each do |user| %> <tr> <td> <%= attachment_imege_tag(user, :profile_image, :fill, 50, 50, fallback: "no-image-mini.jpg") %>
フォームタグ <!--検索フォーム --> <% if user_signed_in? %> <%= form_tag(search_path, method: :get) do %> <%=text_field_tag 'word' %> <%= select_tag 'range', options_for_select([['---選択してください---',''], ['User', '1'], ['Book', '2']]) %> <%= select_tag 'search', options_for_select([['前方一致', 'forword_match'], ['後方一致', 'backword_match'],['完全一致', 'perfect_match'], ['部分一致', 'partial_match']]) %> <%= submit_tag '検索' %> <% end %> <% end %> <!--検索フォーム -->
searches controller class SearchesController < ApplicationController def search @range = params[:range] search = params[:search] word = params[:word] if @range == '1' @user = User.search(params[:search],params[:word]) else @book = Book.search(search,word) end end end
user.rb def self.search(search,word) if search == "forword_match" User.where(["name LIKE?", "#{word}%"]) elsif search == "backword_match" User.where(["name LIKE?", "%#{word}"]) elsif search == "perfect_match" User.where(["#{word}"]) elsif search == "partial_match" User.where(["name LIKE?", "%#{word}%"]) else User.all end end
book.rb def self.search(search,word) if search == 'forword_match' @book = Book.where('title LIKE?', '#{word}%') elsif search == 'backword_match' @book = Book.where('title LIKE?', '%#{word}') elsif search == 'perfect_match' @book = Book.where('#{word}') elsif search == 'partial_match' @book = Book.where('title LIKE?','%#{word}%') else @book = Book.all end end
search.html.erb <% if @range == '1' %> <h2>Users search for #{word}</h2> <table class="table"> <thead> <tr> <th>image</th> <th>name</th> </tr> </thead> <tbody> <% @user.each do |user| %> <tr> <td> <%= attachment_imege_tag(user, :profile_image, :fill, 50, 50, fallback: "no-image-mini.jpg") %> </td> <td><%= user.name %></td> <td><%= link_to 'show', user_path(user) %></td> </tr> <% end %> </tbody> </table> <% else %> <h2>Books search for #{word}</h2> <table class="table"> <thead> <tr> <th>image</th> <th>name</th> </tr> </thead> <tbody> <% @book.each do |book| %> <tr> <td> <%= attachment_imege_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-mini.jpg") %> </td> <td><%= book.user.name %></td> <td><%= link_to 'show', user_path(book.user) %></td> </tr> <% end %> </tbody> </table> <% end %>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/20 08:30