質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

770閲覧

検索機能を実装したいが、該当なしになってしまう

kenta34344

総合スコア5

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/10/17 00:54

編集2021/10/19 12:33

前提・実現したいこと

tagでの検索を実装したい。しかし詮索結果にヒットしなくて常に該当なしになってしまう。

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

エラーメッセージ

該当のソースコード

stockitemcontroller

1class StockItemsController < ApplicationController 2 3 before_action :search_stock_item, only: [:index, :search] 4 5 def index 6 @stock_items = StockItem.all 7 end 8 9 def new 10 @stock_item = ItemsTag.new 11 end 12 13 def create 14 @stock_item = ItemsTag.new(stock_item_params) 15 if @stock_item.save 16 return redirect_to root_path 17 else 18 render "new" 19 end 20 end 21 22 def edit 23 @stock_item = StockItem.find(params[:id]) 24 end 25 26 def update 27 if @stock_item.update(stock_item_params) 28 redirect_to contacts_path 29 else 30 render :edit 31 end 32 end 33 34 def destroy 35 @stock_item = StockItem.find(params[:id]) 36 if @stock_item.destroy 37 redirect_to root_path 38 else 39 render :index 40 end 41 end 42 43 def search 44 @results = @q.result.includes(:tag) 45 binding.pry 46 end 47 48 private 49 50 def stock_item_params 51 params.require(:items_tag).permit(:stock_item_manufacturer, :stock_item_name, :stock_item_standard, :stock_item_strage_condition, :stock_item_description, :image, :tag_word) 52 end 53 54 def search_stock_item 55 @q = StockItem.ransack(params[:q]) 56 end 57 58end 59

stockitemrb

1class StockItem < ApplicationRecord 2 3 has_one_attached :image 4 has_many :item_connects, dependent: :destroy 5 has_many :tags, through: :item_connects 6 7 extend ActiveHash::Associations::ActiveRecordExtensions 8 belongs_to :genre 9end

tagrb

1class Tag < ApplicationRecord 2 3 has_many :item_connects 4 has_many :stock_items, through: :item_connects 5end 6

itemstagrb

1include ActiveModel::Model 2 attr_accessor :stock_item_manufacturer, :stock_item_name, :stock_item_standard, :stock_item_strage_condition, :stock_item_description, :image, :tag_word 3 4 with_options presence: true do 5 validates :stock_item_manufacturer 6 validates :stock_item_name 7 validates :stock_item_standard 8 validates :stock_item_strage_condition 9 validates :stock_item_description 10 validates :image 11 validates :tag_word 12 end 13 14 def save 15 16 stock_item = StockItem.create(stock_item_manufacturer: stock_item_manufacturer, stock_item_name: stock_item_name, stock_item_standard: stock_item_standard, stock_item_strage_condition: stock_item_strage_condition, stock_item_description: stock_item_description, image: image) 17 18 tag = Tag.where(tag_word: tag_word).first_or_initialize 19 tag.save 20 21 ItemConnect.create(stock_item_id: stock_item.id, tag_id: tag.id) 22 23 end 24 25end

newhtml

1<div class='registration-main'> 2 <div class='form-wrap'> 3 <div class="form__wrapper"> 4 <h2 class="page-heading"> 在庫商品の投稿 </h2> 5 6 <%= form_with model: @stock_item, url: stock_items_path, local: true do |f| %> 7 <div class="field"> 8 <%= f.label :stock_item_manufacturer, "メーカー名", class: :form_name %><br /> 9 <%= f.text_field :stock_item_manufacturer, class: :form_text, id:"item_manufacturer" %> 10 </div> 11 12 <div class="field"> 13 <%= f.label :stock_item_name, "商品名", class: :form_name %><br /> 14 <%= f.text_field :stock_item_name, class: :form_text, id:"new_item_name" %> 15 </div> 16 17 <div class="field"> 18 <%= f.label :stock_item_standard, "規格", class: :form_name %><br /> 19 <%= f.text_field :stock_item_standard, class: :form_text, id:"item_standard" %> 20 </div> 21 22 <div class="field"> 23 <%= f.label :stock_item_strage_condition, "保存方法", class: :form_name %><br /> 24 <%= f.text_field :stock_item_strage_condition, class: :form_text, id:"item_strage_condition" %> 25 </div> 26 27 <div class="field"> 28 <%= f.label :stock_item_description, "商品説明", class: :form_name %><br /> 29 <%= f.text_area :stock_item_description, class: :form__text, id:"item_description" %> 30 </div> 31 32 <div class="field"> 33 <%= f.label :tag_word, "タグ", class: :form_name %><br /> 34 <%= f.collection_select(:tag_word, Genre.all, :name, :name, {}, {class:"form__text"}) %> 35 </div> 36 37 <div class="parallel"> 38 <div class="field"> 39 <%= f.label :image, "新商品の画像", class: :form_name %><br /> 40 <%= f.file_field :image, id:"item_image", class: :form_name %> 41 </div> 42 </div> 43 44 <div class="actions"> 45 <%= f.submit "保存する", class: :send, class: :form_name %> 46 </div> 47 <div id="image-list" ></div> 48 <% end %> 49 </div> 50 </div> 51</div> 52

indexhhml

1<%# 商品の検索 %> 2 <div class='stock_item_search'> 3 <h1> 4 在庫商品検索 5 </h1> 6 <%= search_form_for @q, url: stock_items_search_path do |f| %> 7 <%= f.label :tag_word_eq, 'タグ' %> 8 <%= f.collection_select :stock_item_manufacturer_eq, Genre.all, :name, :name, include_blanck: '指定なし' %> 9 <%= f.submit '検索'%> 10 <% end %> 11 </div> 12 13 <%# 在庫商品一覧 %> 14 <div class='item-contents'> 15 <ul class='item-lists'> 16 <% @stock_items.each do |stock_item| %> 17 <div class='list'> 18 <div class='item-img-content'> 19 <ul class="item-index"> 20 <li><%= stock_item.stock_item_manufacturer %></li> 21 <li><%= stock_item.stock_item_name %></li> 22 </ul> 23 <%= image_tag stock_item.image, class: "item-img"%> 24 </div> 25 <div class='tag'> 26 <li class='tag-lists'> 27 <% stock_item.tags.each do |tag| %> 28 <p><%= tag.tag_word %></p> 29 <% end %> 30 </li> 31 </div> 32 <div class="stock_item-edit"> 33 <% if user_signed_in? && current_user.admin? %> 34 <%= link_to "編集する", edit_stock_item_path(stock_item.id),method: :get, class: "btn btn-outline-secondary" %> 35 <%= link_to "削除する", stock_item_path(stock_item.id),method: :delete, class: "btn btn-outline-secondary" %> 36 <% end %> 37 </div> 38 </div> 39 <% end %> 40 </ul> 41 </div> 42 <%# /在庫商品一覧 %>

searchhtml

1<h1> 2 検索結果 3</h1> 4 5<% if @results.length !=0 %> 6 <% @results.each do |result| %> 7 <div class='list'> 8 <div class='item-img-content'> 9 <%= image_tag stock_item.image, class: "item-img"%> 10 </div> 11 <div class='tag'> 12 <li class='tag-lists'> 13 <% stock_item.tags.each do |tag| %> 14 <p><%= tag.tag_word %></p> 15 <% end %> 16 </li> 17 </div> 18 </div> 19 <% end %> 20<% else %> 21 該当する商品はありません 22<% end %> 23<br> 24<%= link_to 'トップページへ戻る', root_path %>

試したこと

def search
@results = @p.result.includes(:tag)
binding.pry
end
ここでbinding.pryで止めると
pry(#<StockItemsController>)> params
=> <ActionController::Parameters {"q"=><ActionController::Parameters {"stock_item_manufacturer_eq"=>"湯せん"} permitted: false>, "commit"=>"検索", "controller"=>"stock_items", "action"=>"search"} permitted: false>
となります。
tagの湯せんを検索したいですが、stock_item_manufacturer_eqとなっており、ここに問題があるのでは?と思ってますが、何故そうなるかが分かりません。
よろしくお願いします。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2021/10/17 00:59

ビューはどんなコードを書いていますか?
kenta34344

2021/10/17 01:14

ビューを追加しました。 確認をお願いします。
guest

回答1

0

q が出てくるということは ransack を用いた検索を実装しているように思えます。
すると問題が2つ+懸念1
1."stock_item_manufacturer_eq"=>"湯せん" というのは StokItem manufacturer の値が "湯せん" であるもの という意味ですので
Ransack を使うという意味では、合っていますが
StockItemの関連のTagを使って検索するという意味では合っていません。
2. Controllerの記述で、ransackを使う検索 を行う記述がありません
3. action search がうまく行ったとして、その結果を表示するview用意してありますか?
ここは render :index するのが普通かと、

Tag にある 湯せん が入っているカラム名を COLUMN とすると
2. は stock_item_tag_COLUMN_eq です。(tagsだったかも、、、)
Controllerでどう扱うのか、は検索すれば色々出てくるかと思います例えばとか。
なお、indexで before_action 無視して、 @stock_items = StockItem.all で取り直しているので、このままでは常に全検索になります

投稿2021/10/17 01:21

winterboum

総合スコア23416

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kenta34344

2021/10/18 11:46

回答有難うございます。 Tagsテーブルのカラム名は、tag_wordになるんですが、stock_item_tag_word_eqやtags、tag_word_eqとしてみても NoMethodError in StockItems#index Showing /Users/watanabekenta/orijinals/n-web_app/app/views/stock_items/index.html.erb where line #87 raised: undefined method `stock_item_tag_word_eq' for #<Ransack::Search:0x00007fd2d8309b68> Extracted source (around line #87): 85 86 87 88 89 90 <%= search_form_for @p, url: search_stock_items_path do |f| %> <%= f.label :tag_word_eq, 'タグ' %> <%= f.collection_select :stock_item_tag_word_eq, Genre.all, :id, :name, include_blanck: '指定なし' %> <%= f.submit '検索'%> <% end %> </div> となってしまいます。 なぜtag_wordが使えないのでしょうか?
winterboum

2021/10/18 12:32

p と q が統一されてません
kenta34344

2021/10/19 12:24

pとqは統一しましたがエラーは変わりません。アソシエーション等はミスないと思いますが、フォームオブジェクトを使用しているからclassでうまく繋がらずエラーが出るんでしょうか?
winterboum

2021/10/19 12:27

どう統一したかcodeを載せてください。修正してでもよいですが。
kenta34344

2021/10/19 12:36

コントローラーのdef searchのpをqに、def search_stock_itemのpをqに ビューのsearch_form_for @q に変更しました。
winterboum

2021/10/19 12:59

正しくなおしたか、がわからないので、codeで見たいのです
kenta34344

2021/10/19 13:07

上の該当ソースコードも修正しました。
winterboum

2021/10/19 21:20

「エラーは変わりません」とありますが、質問本文にはエラーは書いてなく、 「常に該当なしになってしまう」とあります。どちらが正しい?
kenta34344

2021/10/20 04:29

NoMethodError in StockItems#index Showing /Users/watanabekenta/orijinals/n-web_app/app/views/stock_items/index.html.erb where line #87 raised: undefined method `tag_word_eq' for #<Ransack::Search:0x00007fa347c9ef68> Extracted source (around line #87): 85 86 87 88 89 90 <%= search_form_for @q, url: search_stock_items_path do |f| %> <%= f.label :tag_word_eq, 'タグ' %> <%= f.collection_select :tag_word_eq, Genre.all, :id, :name, include_blanck: '指定なし' %> <%= f.submit '検索'%> <% end %> </div> こちらのエラーが出ます。
winterboum

2021/10/20 05:27

tag_word_eq この意味は 「(StockItemの関連) tag の wordカラムの値」 を意味します。 tag に wordってありますか? (has_manyだから tag_word ではなく tags_word だったかも)
kenta34344

2021/10/20 11:05

Tagsテーブルにtag_wordカラムがあります。 tags_tag_wordにてエラーは出なくなりました。ただ未だに検索結果には該当なしです。改めて自分で調べてみて分からなければ相談させてください。
winterboum

2021/10/20 13:16

@q = StockItem.joins(:tags).ransack(params[:q]) かなぁ
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問