お世話になっております。
下記の件、知見がある方がいらっしゃいましたらご教示お願いします。
#知りたいこと
railsのセレクトボックスで選択肢が重複するのを防ぎたいです。
都道府県を選択するセレクトボックスがあり、ユーザーが投稿した住所が都道府県の選択肢に含まれているようになっています。
下記のように同じ都道府県の投稿があると選択肢の中に含まれてしまいます。
![]
#試したこと
index.html.erb
1: 2: 3<%= f.collection_select :address_prefectures_eq, Post.all, :id, :address_prefectures, :include_blank => '指定なし'%> 4: 5:
colection_select
のオブジェクト配列を入れる値のところにPost.all
を格納していたため、
@posts=Post.all
と定義し、@posts.select(address_prefectures).distinct,
や@posts.uniq
と書きましたが重複を防ぐことはできませんでした。
#関連ファイル
#index.html.erb <% provide(:title, '投稿一覧画面') %> <div class="row"> <div class="col-md-1"> </div> <div class="col-md-10"> <button class="search btn btn-default">絞り込み検索</button> <div class="search box"> <%= search_form_for(@q, url:post_path) do |f|%> お店の名前 <%= f.search_field :store_name_cont%> 営業時間 <%= f.time_field :time_start_gteq %>〜 <%= f.time_field :time_end_lteq %> 都道府県 <%= f.collection_select :address_prefectures_eq, @postall.distinct, :id, :address_prefectures, :include_blank => '指定なし'%> 並び替え <%= f.select :sorts, {並び替え: "", いいねの多い順: "likes_count desc" , 投稿の新しい順: "created_at desc"} %> <p>環境</p> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default" > <%= f.check_box :wifi_eq_any, { multiple: true }, true, false %><%= image_tag 'wifi.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= f.check_box :plug_eq_any, { multiple: true }, true, false %><%= image_tag 'plug.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= f.check_box :tabacco_eq_any , { multiple: true } , true, false %><%= image_tag 'tabacco.png' , :size => '50x40'%> </label> </div> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default"> <%= f.check_box :card_eq_any , { multiple: true } , true, false %><%= image_tag 'card.png' , :size => '50x40'%> </label> </div> <%= f.submit %> <%end%> </div> <div class="Allpost"> <% @posts.each do |post| %> <a href="/post/<%= post.id %>"> <div class="Allpost_detail"> <div class="post_image"> <%= image_tag post.image, :size =>'130x130'%> </div> <div class="post_content"> <p>お店の名前:<%= post.store_name %></p> <p>住所:<%= post.address_prefectures %><%= post.address %></p> <p>営業時間:<%= post.time_start %>〜<%= post.time_end %></p> </div> </div> </a> <% end %> </div> <%= will_paginate class: "center"%> </div> <div class="col-md-1"> </div> </div>
#posts_controller.rb class PostsController < ApplicationController protect_from_forgery except: :create before_action :post_params, only: %i[confirm create] before_action :correct_user, only: :destroy def new @post = Post.new end def create @post = Post.new(post_params) @post.user = current_user @post.save! flash[:success] = '投稿されました。' redirect_to action: 'index' end def show @post = Post.find(params[:id]) end def confirm @post = Post.new(post_params) end def index @post = Post.new @postall = Post.all @like = Like.new @q = Post.ransack(params[:q]) @posts = @q.result(distinct: true).paginate(page: params[:page], per_page: 6) end def destroy @post.destroy flash[:success] = '投稿が削除されました。' redirect_to request.referrer || root_url end private def post_params params.require(:post).permit(:store_name, :address, :address_prefectures, :image, :time_start, :time_end, :wifi, :plug, :tabacco, :card, :evaluation1, :evaluation2, :evaluation3, :evaluation4, :evaluation5) end def search_params params.require(:q).permit(:store_name_cont, :sorts) end def correct_user @post = current_user.posts.find_by(id: params[:id]) redirect_to root_url if @post.nil? end end
#環境
Rails 5.1.6
Ruby 2.7.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。