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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

829閲覧

simple_formでタグを表示させたい

rat_mouse

総合スコア17

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/10/27 06:38

編集2021/10/27 06:55

やりたいこと
railsでadmin側から送信したタグ(target)をuser側で表示させたい

起きている問題
admin側でtargetカラムを複数選択して送信したいが、下記の問題が起きている
・空の要素が送信される
・送信した要素がまとまって送信される

送信される情報

Started POST "/recruitments" for 122.131.139.104 at 2021-10-27 15:04:41 +0900 Processing by RecruitmentsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"0fiKNHBc1CBXvd/9UHlmpNutDfZ8oa1+h0TNNrEM3NQ6vSmxYTDWcGLsC7g0daF8JWZVUTCgpR1+cyLrKjRp+A==", "recruitment"=>{"status"=>"", "created_at"=>"2021/10/27", "creator_id"=>"hoge", "target"=>["", "小学生", "中学生"], "start_date"=>"2021/10/27", "end_date"=>"2021/10/30", "title"=>"テスト", "content"=>"<div>っっs</div>"}}

該当箇所

"target"=>["", "小学生", "中学生"]

期待する挙動

"target"=>["小学生"], ["中学生"]

よろしくお願いいたします。

コード
admin側
admin/app/views/recruitment/_form.html.slim

= simple_form_for model, html: { class: 'form-horizontal js-editing' } do |f| .container .row .col-sm-12.no-padding .ibox .ibox-title.article_title h5 募集要項の投稿 .ibox-content.p-xxs.col-sm-6 = f.input :status, as: :select, collection: ["delivered","private","draft"], input_html: {value: "draft"} = f.input :created_at, as: :date, input_html: {value: Time.now} = f.input :creator_id, as: :select, collection: User.all.pluck(:name).uniq, input_html: {value: current_user.name} = f.input :target, as: :select, collection: ["小学生", "中学生", "高校生", "社会人", "全員"], input_html: {multiple: true, default: "小学生"} = f.input :start_date = f.input :end_date - content_for :footer .col-sm-3 = render 'shared/action/form_save_button'

admin/app/controllers/recruitment.controller.rb

class RecruitmentsController < ApplicationController before_action :set_recruitment, only: [:show, :edit, :update, :destroy] respond_to :html def index @q = Recruitment.where(status: 'delivered').order(created_at: :desc).ransack(params[:q]) @recruitments = RansackUtil.paged_scope(@q, params).per(18) @current_time = Time.current respond_with(@recruitments) end def show respond_with(@recruitment) end def new @recruitment = Recruitment.new respond_with(@recruitment) end def edit end def create @recruitment = Recruitment.new(recruitment_params) @recruitment.save respond_with(@recruitment) end def update @recruitment.update(recruitment_params) respond_with(@recruitment) end def destroy @recruitment.destroy respond_with(@recruitment) end private def set_recruitment @recruitment = Recruitment.find(params[:id]) end def recruitment_params params.require(:recruitment).permit(:title, :content, :target, :start_date, :end_date) end end

admin/app/models/recruitment.rb

class RecruitmentsController < ApplicationController responders :flash, :http_cache before_action :set_resource, only: %i[show destroy] before_action :init_resource, only: %i[new create] before_action :edit_resource, only: %i[edit update] def index @q =Recruitment.all.order(created_at: :desc).ransack(params[:q]) @recruitments = RansackUtil.paged_scope(@q, params) respond_with(@recruitments) end def show respond_with(@recruitment) end def new respond_with(@recruitment) end def edit respond_with(@recruitment) end def create @recruitment.save respond_with(@recruitment) end def update @recruitment.save respond_with(@recruitment) end def destroy @recruitment.destroy respond_with(@recruitment) end private def set_resource @recruitment = Recruitment.with_deleted.find(params[:id]) end def init_resource attributes = params[:recruitment].blank? ? {} : resource_params @recruitment = Recruitment.new attributes end def edit_resource set_resource return if params[:recruitment].blank? @recruitment.attributes = resource_params end def resource_params params.require(:recruitment).permit! end end

user側
user/app/views/recruitments/index.html.slim

.pageBox.mb-5.mt-2 .row .pageBox.flex_blog .col-md-8 h1.wow.fadeIn.animated - @recruitments.each do |recruitment| - if recruitment.end_date > @current_time h2.wow.fadeIn.animated = recruitment.title .archive-entries .archive-entry-header .archive-date = recruitment.target .archive-entry-body .entry-description = image_tag recruitment.top_image.attachment.service_url if recruitment.top_image.attached? == recruitment.content .social-area.mb-2 .border_end

user/app/controllers/recruitment.controller.rb

class RecruitmentsController < ApplicationController before_action :set_recruitment, only: [:show, :edit, :update, :destroy] respond_to :html def index @q = Recruitment.where(status: 'delivered').order(created_at: :desc).ransack(params[:q]) @recruitments = RansackUtil.paged_scope(@q, params).per(18) @current_time = Time.current respond_with(@recruitments) end def show respond_with(@recruitment) end def new @recruitment = Recruitment.new respond_with(@recruitment) end def edit end def create @recruitment = Recruitment.new(recruitment_params) @recruitment.save respond_with(@recruitment) end def update @recruitment.update(recruitment_params) respond_with(@recruitment) end def destroy @recruitment.destroy respond_with(@recruitment) end private def set_recruitment @recruitment = Recruitment.find(params[:id]) end def recruitment_params params.require(:recruitment).permit(:title, :content, :target, :start_date, :end_date) end end

user/app/models/recruitment.rb

class Recruitment < ApplicationRecord has_rich_text :content has_one_attached :top_image acts_as_paranoid stampable with_deleted: true scope :order_seq, -> { order(:seq_order) } after_initialize :default_values, if: :new_record? private def default_values # self.status ||= :preparing # self.contract_at ||= Time.zone.today end end

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

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

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

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

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

guest

回答1

0

ベストアンサー

"target"=>["小学生"], ["中学生"] これはできません。送れるのは一つの要素です。これはふたつ の要素ですから。
ですのでできても "target"=>["小学生", "中学生"] です。

投稿2021/10/27 07:01

winterboum

総合スコア23284

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問