前提・実現したいこと
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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。