前提・実現したいこと
現在、deviseを用いてログイン機能を実装しています。
新規登録時に1ユーザに対して複数の情報(今回は都道府県)をカラムに格納しようとしています。
都道府県の情報はactive_hashを用いて、作成したprefecture.rb内に記述。
新規登録画面でcollection_check_boxesによって複数選択し、Userテーブルのareaカラムに配列で格納されることを期待しています。
しかし、いざ登録をしてみると以下のようにareaカラム内に意図せず不要なバックスラッシュが登録されており、これに対してどのように対応するべきか悩んでおります。
■POST実行時のターミナル出力 Started POST "/users" for 10.0.2.2 at 2020-10-11 17:59:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Users::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"wly0Fcgoov/6i4OT73agEOO74zhj35Mqpg5T4ezGNHvsEb1YJky693n0QgiuxUoE/zMgv4AyslMxfEzgzIQXKA==", "user"=>{"name"=>"test1", "email"=>"test1@gmail.com", "gender"=>"man", "age"=>"20", "area"=>["", "11", "12", "13"], "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"新規登録"} (0.1ms) begin transaction ↳ /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.4/lib/active_record/log_subscriber.rb:98 User Exists (1.6ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test1@gmail.com"], ["LIMIT", 1]] ↳ /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.4/lib/active_record/log_subscriber.rb:98 User Create (4.1ms) INSERT INTO "users" ("email", "encrypted_password", "name", "gender", "age", "area", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["email", "test1@gmail.com"], ["encrypted_password", "$2a$12$31dBC.QAw2fkvgqakT63Ru6wc9XXQOkmD9ei5F51hMTXLdJfuPvVe"], ["name", "test1"], ["gender", "man"], ["age", 20], ["area", "[\"\", \"11\", \"12\", \"13\"]"], ["created_at", "2020-10-11 17:59:41.260418"], ["updated_at", "2020-10-11 17:59:41.260418"]] ↳ /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.4/lib/active_record/log_subscriber.rb:98 (4.9ms) commit transaction ↳ /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.4/lib/active_record/log_subscriber.rb:98 Redirected to http://localhost:3000/ Completed 302 Found in 654ms (ActiveRecord: 10.7ms) ■Userテーブルの内容を確認 $rails console [1]pry(main)>User.all User Load (2.2ms) SELECT "users".* FROM "users" => [#<User id: 1, email: "test1@gmail.com", name: "test1", introduction: nil, gender: "man", age: 20, area: "[\"\", \"11\", \"12\", \"13\"]", part: nil, genre: nil, profile_image_id: nil, is_member: true, created_at: "2020-10-11 17:59:41", updated_at: "2020-10-11 17:59:41">]
該当のソースコード
schema.rb ActiveRecord::Schema.define(version: 2020_10_10_053746) do create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.text "introduction" t.string "gender" t.integer "age" t.string "area" t.string "part" t.string "genre" t.string "profile_image_id" t.boolean "is_member", default: true, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :prefecture end
prefecture.rb class Prefecture < ActiveHash::Base self.data = [ {id: 1, name: '北海道'}, {id: 2, name: '青森県'}, {id: 3, name: '岩手県'}, {id: 4, name: '宮城県'}, {id: 5, name: '秋田県'}, {id: 6, name: '山形県'}, {id: 7, name: '福島県'}, {id: 8, name: '茨城県'}, {id: 9, name: '栃木県'}, {id: 10, name: '群馬県'}, {id: 11, name: '埼玉県'}, {id: 12, name: '千葉県'}, {id: 13, name: '東京都'}, {id: 14, name: '神奈川県'}, {id: 15, name: '新潟県'}, {id: 16, name: '富山県'}, {id: 17, name: '石川県'}, {id: 18, name: '福井県'}, {id: 19, name: '山梨県'}, {id: 20, name: '長野県'}, {id: 21, name: '岐阜県'}, {id: 22, name: '静岡県'}, {id: 23, name: '愛知県'}, {id: 24, name: '三重県'}, {id: 25, name: '滋賀県'}, {id: 26, name: '京都府'}, {id: 27, name: '大阪府'}, {id: 28, name: '兵庫県'}, {id: 29, name: '奈良県'}, {id: 30, name: '和歌山県'}, {id: 31, name: '鳥取県'}, {id: 32, name: '島根県'}, {id: 33, name: '岡山県'}, {id: 34, name: '広島県'}, {id: 35, name: '山口県'}, {id: 36, name: '徳島県'}, {id: 37, name: '香川県'}, {id: 38, name: '愛媛県'}, {id: 39, name: '高知県'}, {id: 40, name: '福岡県'}, {id: 41, name: '佐賀県'}, {id: 42, name: '長崎県'}, {id: 43, name: '熊本県'}, {id: 44, name: '大分県'}, {id: 45, name: '宮崎県'}, {id: 46, name: '鹿児島県'}, {id: 47, name: '沖縄県'} ] include ActiveHash::Associations has_many :users end
application_controlle.rb class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :gender, :age, :part, :genre, :area => []]) end end
registrations/new.html.erb <h2>新規ユーザー登録</h2> <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= render "users/shared/error_messages", resource: resource %> <table class="table"> <thead></thead> <tbody> <tr> <td> <div class="field"> <%= f.label :name, "ニックネーム" %><br /> <%= f.text_field :name, autofocus: true, autocomplete: "name" %> </div> </td> </tr> <tr> <td> <div class="field"> <%= f.label :email, "メールアドレス" %><br /> <%= f.email_field :email, autocomplete: "email" %> </div> </td> </tr> <!-- 該当項目の入力箇所 --> <tr> <td> <div class="field"> <%= f.label :area, "活動地域" %><br /> <%= f.collection_check_boxes :area, Prefecture.all, :id, :name do |area| %> <%= area.label {area.check_box + area.text} %> <% end %> </div> </td> </tr> <!-- /該当項目の入力箇所 --> <tr> <td> <div class="field"> <%= f.label :password, "パスワード" %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> 文字以上)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "new-password" %> </div> </td> </tr> <tr> <td> <div class="field"> <%= f.label :password_confirmation, "パスワード(確認用)" %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> </td> </tr> </tbody> </table> <div class="actions"> <%= f.submit "新規登録" %> </div> <% end %> <h4><mark style="background-color: #F8F8F8F8;">既に登録済みの方</mark></h4> <%= link_to "こちら", new_session_path(resource_name) %>からログインしてください。
web上で参考情報等を検索してみましたが、これといっためぼしい情報を見つけられませんでした。
初学者につき対応すべき箇所の検討もついておらず、御助力いただきたく存じます。
補足情報(FW/ツールのバージョンなど)
・macOS 10.15.6
・Ruby 2.5.7
・Rails 5.2.4.4
・gem devise
・gem active_hash
回答1件
あなたの回答
tips
プレビュー