実現したいこと
railsで作成しているアプリにタグ検索機能を追加したい
起きている問題
viewの方でcheckboxで選択したタグをcontroller側で取得し、データを探す際、「Unpermitted parameter」となってしまう。
ログ
「東京」だけを選択した場合。
Started GET "/shops/select_prefecture?utf8=%E2%9C%93&search%5Bprefecture%5D%5B%5D=%E6%9D%B1%E4%BA%AC&commit=%E6%A4%9C%E7%B4%A2" for ::1 at 2021-05-14 18:05:46 +0900 Processing by ShopsController#select_prefecture as HTML Parameters: {"utf8"=>"✓", "search"=>{"prefecture"=>["東京"]}, "commit"=>"検索"} Unpermitted parameter: :prefecture Unpermitted parameter: :prefecture Rendering shops/index.html.erb within layouts/application Tag Load (0.6ms) SELECT `tags`.* FROM `tags` ↳ app/views/shops/index.html.erb:42 Shop Load (0.6ms) SELECT `shops`.* FROM `shops` WHERE (prefecture LIKE '%{}%') ↳ app/views/shops/index.html.erb:58 Rendered shops/index.html.erb within layouts/application (12.2ms) Rendered layouts/_header.html.erb (1.4ms) Completed 200 OK in 85ms (Views: 79.9ms | ActiveRecord: 1.2ms)
view
index.html.erb
1<h3>店舗一覧</h3> 2<div container-fluid> 3 <div class="row"> 4 <div class="col-3"> 5 <table class="table"> 6 <%= form_with scope: :search, url: s_pref_path, local: true, method: 'get' do |f| %> 7 <tr> 8 <th>関東</th> 9 </tr> 10 <tr> 11 <td><%= f.check_box :prefecture, {multiple: true}, '東京', false%> 12 <%= f.label :prefecture, "東京" %> 13 </td> 14 </tr> 15 <tr> 16 <td><%= f.check_box :prefecture, {multiple: true}, '神奈川', false%> 17 <%= f.label :prefecture, "神奈川" %></td> 18 </tr> 19 <tr> 20 <td><%= f.check_box :prefecture, {multiple: true}, '埼玉', false%> 21 <%= f.label :prefecture, "埼玉" %></td> 22 </tr> 23 <tr> 24 <td><%= f.check_box :prefecture, {multiple: true}, '千葉', false%> 25 <%= f.label :prefecture, "千葉" %></td> 26 </tr> 27 <tr> 28 <td><%= f.check_box :prefecture, {multiple: true}, '茨城', false%> 29 <%= f.label :prefecture, "茨城" %></td> 30 </tr> 31 <tr> 32 <td><%= f.check_box :prefecture, {multiple: true}, '栃木', false%> 33 <%= f.label :prefecture, "栃木" %></td> 34 <tr> 35 <td><%= f.check_box :prefecture, {multiple: true}, '群馬', false%> 36 <%= f.label :prefecture, "群馬" %></td> 37 </tr> 38 39 <tr> 40 <th>特徴タグ</th> 41 </tr> 42 <% @tags.each do |tag| %> 43 <tr> 44 <td><%= f.check_box :tag, {multiple: true}, tag.id, nil %><%= f.label :tag, tag.name %></td> 45 </tr> 46 <% end %> 47 <tr> 48 <td><%= f.submit "検索"%></td> 49 </tr> 50 <% end %> 51 </table> 52 </div> 53 54 <div class="col-9"> 55 <div class="row"> 56 <div class="container mt-4"> 57 <div class="row w-100"> 58 <% @shops.each do |shop| %> 59 <% if shop.del_flg != 1 %> 60 <div class="col-3"> 61 <% if shop.image? %> 62 <%= link_to s_show_path(shop) do %> 63 <%= image_tag shop.image.url, class: 'img-thumbnail' %> 64 <% end %> 65 <% end %> 66 <div class="row"> 67 <div class="col-12"> 68 <p class="mt-2"><%= shop.name %></p> 69 <%= link_to "店舗詳細", s_show_path(shop) %> 70 </div> 71 </div> 72 </div> 73 <% end %> 74 <% end %> 75 </div> 76 </div> 77 </div> 78 </div> 79 </div> 80</div>
controller
class ShopsController < ApplicationController before_action :set_shop, except: [:new, :create, :index, :zip, :select_prefecture] before_action :set_owner, only: [:create, :show] before_action :before_login_owner, only: [:new, :create, :edit, :update, :destroy] before_action :correct_owner, only: [:edit, :update, :destroy] ・・・ def select_prefecture prefecture_params = params.require(:search).permit(:prefecture) @shops = Shop.where('prefecture LIKE ?', "%#{prefecture_params}%") # @shops = Shop.search(@search_params).includes(:tag) # tag_params = params.require(:search).permit(:tag) render 'index' end
model
Shops テーブルはTag_to_shopsテーブルを中間テーブルとして tagテーブルと多対多で繋がってます。
class Shop < ApplicationRecord mount_uploader :image, ImageUploader belongs_to :owner has_many :calendars has_many :chats, foreign_key: :shop_id, dependent: :destroy has_many :availables, foreign_key: :shop_id, dependent: :destroy has_many :tag_to_shops, foreign_key: :shop_id, dependent: :destroy has_many :tag, through: :tag_to_shops validates :name, presence: true, length: { maximum: 50 } validates :zip_code, presence: true validates :prefecture, presence: true validates :city, presence: true validates :tel, presence: true validates :station, presence: true end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。