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

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

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

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

Ruby

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

Q&A

0回答

1798閲覧

collection_check_boxesで困ったこと

Kengo_jeb

総合スコア9

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2020/05/26 05:21

collection_check_boxesを使用してチェックボックス機能を作成しました。初期の値はデーターベースに配列として格納できていますが、edit画面ではその情報を反映(チェックすボックスにチェックされている状態)させることができません。
値を保持した状態で尚且つedit画面では、チェックが着くようにしたいのですが、下記サイトを参考にしてもできませんでした。
もし分かる方が見えましたら教えていただけませんか?。

補足
私がやろうとしている内容としては、new画面で値を保持する操作(チェックボックスにチェックした状態)を行っているので、その情報をedit画面でも反映させる機能を実装させたいです。

参考ページ
http://www.letmefix-it.com/2017/08/11/how-to-use-collection_check_boxes-ruby-on-rails/

new.html.haml コード .main %h2.main_title 配色を選択して下さい = form_with model: @product, class: :form, local: true do |form| #form_with model: モデルクラスのインスタンス do |form| .main_new .main_errors = render 'layouts/flash_messages' .head_color %label ヘッドカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :head_color, Color.all, :color_name, :color_display, include_hidden: false, class: "input-default" .upper_body-color %label ボディーカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :boy_color, Color.all, :color_name, :color_display, include_hidden: false, class: "input-default" .leg-color %label レッグカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :leg_color, Color.all, :color_name, :color_display, include_hidden: false, class: "input-default" .shose-color %label レッグカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :shoes_color, Color.all, :color_name, :color_display, include_hidden: false, class: "input-default" .post_new = form.submit '登録する', class: :form__btn
edit.html.haml コード .main %h2.main_title 配色を選択して下さい = form_with model: @product, class: :form, local: true do |form| #form_with model: モデルクラスのインスタンス do |form| .main_new .main_errors = render 'layouts/flash_messages' .head_color %label ヘッドカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :head_color, Color.where(params[:color_name]), :color_name, :color_display, {checked: @product_head_array.map(&:to_param), include_hidden: false}, class: "input-default" .upper_body-color %label ボディーカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :boy_color, Color.where(params[:boy_color]), :color_name, :color_display, {checked: @product_body_array.map(&:to_param), include_hidden: false},class: "input-default" .leg-color %label レッグカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :leg_color, Color.where(params[:leg_color]), :color_name, :color_display,{checked: @product_leg_array.map(&:to_param), include_hidden: false},class: "input-default" .shose-color %label レッグカラー %span.form-require 必須 .select-wrap = form.collection_check_boxes :shoes_color, Color.where(params[:shoes_color]), :color_name, :color_display, {checked: @product_shoes_array.map(&:to_param), include_hidden: false},class: "input-default" .post_new = form.submit '登録する', class: :form__btn
index.html.haml コード .main .main_head .head-up#head_color .head-down .main_upper-body1 .upper_body#upper-body-center1 .main_upper-body2 .upper_body#upper-body-left .blank_upper_left1 .upper_body#upper-body-center2 .blank_upper_rigth1 .upper_body#upper-body-rigth .main_upper-body3 .upper-left-hand .blank_upper_left2 .upper_body#upper-body-center3 .blank_upper_rigth2 .upper-rigth-hand2 .main_leg1 .leg#leg-center .main_leg2 .leg#leg_left .blank_leg_center#blank_leg .leg#leg_rigth .main_shoes .shoes#shoes_left .blank_shoes_center .shoes#shoes_rigth %form %input{type: "button", value:"更新" ,onclick:"effect1()"}/ .post_new = link_to "新規登録", new_product_path, class: "btn-default btn-gray" .post_edit = link_to "色編集", "/products/#{@products[0].id}/edit", class: "btn-default btn-gray"
productsController.rb コード class ProductsController < ApplicationController before_action :set_product, only: [:edit, :update] def index @products = Product.last(1) @products.each do |product| head_color = product.head_color boy_color = product.boy_color leg_color = product.leg_color shoes_color = product.shoes_color gon.head_color = JSON.parse(head_color) gon.boy_color = JSON.parse(boy_color) gon.leg_color = JSON.parse(leg_color) gon.shoes_color = JSON.parse(shoes_color) end end def new @product = Product.new @Colors = Color.all end def create @product = Product.new(product_params) if @product.save redirect_to root_path else render :new end end def edit @product_head_array = @product.head_color.split(",") @product_body_array = @product.boy_color.split(",") @product_leg_array = @product.leg_color.split(",") @product_shoes_array = @product.shoes_color.split(",") @Colors = Color.all end def update if @product.update(product_params) redirect_to root_path else flash[:alert] = "配色を選択して下さい!" redirect_to edit_product_path end end private def product_params params.require(:product).permit(head_color:[], boy_color:[], leg_color:[], shoes_color:[]) end def set_product @product = Product.find(params[:id]) end end

DBの状態です
イメージ説明

イメージ説明

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問