解決したいこと
ユーザー登録で都道府県をactive_hashで登録したい。
起こっている問題
都道府県のフォームを設定するとエラーが出たので解決したい。
https://gyazo.com/96a95165941ed8a2c85369be65aab5b9
application_controller.rb
ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :configure_permitted_parameters, if: :devise_controller? 4 5 protected 6 def configure_permitted_parameters 7 devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :email, :password, :birthday, :age, :sex, :prefecture, :bike_name, :avatar_image, :bike_image]) 8 end 9end
models/user.rb
ruby
1class User < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 validates :nickname, :email, presence: true, uniqueness: true 7 validates :password, :birthday, :age, :sex, :prefecture, :bike_name, presence: true, uniqueness: true 8 9 has_many :posts, dependent: :destroy 10 has_many :comments, dependent: :destroy 11 belongs_to_active_hash :prefecture 12end
models/prefecture.rb
ruby
1class Prefecture < ActiveHash::Base 2 self.data = [ 3 {id: 1, name: '北海道'}, {id: 2, name: '青森県'}, {id: 3, name: '岩手県'}, 4 {id: 4, name: '宮城県'}, {id: 5, name: '秋田県'}, {id: 6, name: '山形県'}, 5 {id: 7, name: '福島県'}, {id: 8, name: '茨城県'}, {id: 9, name: '栃木県'}, 6 {id: 10, name: '群馬県'}, {id: 11, name: '埼玉県'}, {id: 12, name: '千葉県'}, 7 {id: 13, name: '東京都'}, {id: 14, name: '神奈川県'}, {id: 15, name: '新潟県'}, 8 {id: 16, name: '富山県'}, {id: 17, name: '石川県'}, {id: 18, name: '福井県'}, 9 {id: 19, name: '山梨県'}, {id: 20, name: '長野県'}, {id: 21, name: '岐阜県'}, 10 {id: 22, name: '静岡県'}, {id: 23, name: '愛知県'}, {id: 24, name: '三重県'}, 11 {id: 25, name: '滋賀県'}, {id: 26, name: '京都府'}, {id: 27, name: '大阪府'}, 12 {id: 28, name: '兵庫県'}, {id: 29, name: '奈良県'}, {id: 30, name: '和歌山県'}, 13 {id: 31, name: '鳥取県'}, {id: 32, name: '島根県'}, {id: 33, name: '岡山県'}, 14 {id: 34, name: '広島県'}, {id: 35, name: '山口県'}, {id: 36, name: '徳島県'}, 15 {id: 37, name: '香川県'}, {id: 38, name: '愛媛県'}, {id: 39, name: '高知県'}, 16 {id: 40, name: '福岡県'}, {id: 41, name: '佐賀県'}, {id: 42, name: '長崎県'}, 17 {id: 43, name: '熊本県'}, {id: 44, name: '大分県'}, {id: 45, name: '宮崎県'}, 18 {id: 46, name: '鹿児島県'}, {id: 47, name: '沖縄県'} 19 ] 20end
views/devise/registrations/new.html.erb
ruby
1<div class="wrapper"> 2 <%= render 'templates/form_header' %> 3 4 <div class="form_container"> 5 <div class="form_container__head"> 6 <h2>アカウント登録</h2> 7 </div> 8 <div class="form_container__form"> 9 <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 10 <%= render "devise/shared/error_messages", resource: resource %> 11 12 <div class="new_user__image_box"> 13 <div class="new_thumbnail"> 14 <%= f.label :image, class: "new_thumbnail__input_image" do %> 15 <i class="far fa-image"></i> 16 <%= f.file_field :image, class: "new_thumbnail__input_image__file" %> 17 <% end %> 18 </div> 19 </div> 20 <div class="new_user__input_box"> 21 22~中略~ 23 24 <div class="field"> 25 <%= f.label :prefecture, "都道府県" %><br /> 26 <%= f.collection_select :prefecture, Prefecture.all, :id, :name, {autofocus: true, include_blank: "選択してください"}, {class: 'select-default', id: 'address_prefecture_name', required: true} %> 27 </div> 28 29 <div class="actions"> 30 <%= f.submit "Sign up" %> 31 </div> 32 </div> 33 <% end %> 34 35 <%= render "devise/shared/links" %> 36 </div> 37 </div> 38 39 <%= render 'templates/form_footer' %> 40</div>
migrate/****_devise_create_users.rb
ruby
1# frozen_string_literal: true 2 3class DeviseCreateUsers < ActiveRecord::Migration[5.2] 4 def change 5 create_table :users do |t| 6 ## Database authenticatable 7 t.string :nickname, null: false, unique: true 8 t.string :email, null: false, unique: true 9 t.string :encrypted_password, null: false, default: "" 10 t.date :birthday, null: false 11 t.string :age, null: false, default: "" 12 t.string :sex, null: false 13 t.integer :prefecture, null: false 14 t.string :bike_name, null: false, default: "" 15 t.string :avatar_image 16 t.string :bike_image 17 18 19 ## Recoverable 20 t.string :reset_password_token 21 t.datetime :reset_password_sent_at 22 23 ## Rememberable 24 t.datetime :remember_created_at 25 26 ## Trackable 27 # t.integer :sign_in_count, default: 0, null: false 28 # t.datetime :current_sign_in_at 29 # t.datetime :last_sign_in_at 30 # t.string :current_sign_in_ip 31 # t.string :last_sign_in_ip 32 33 ## Confirmable 34 # t.string :confirmation_token 35 # t.datetime :confirmed_at 36 # t.datetime :confirmation_sent_at 37 # t.string :unconfirmed_email # Only if using reconfirmable 38 39 ## Lockable 40 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 41 # t.string :unlock_token # Only if unlock strategy is :email or :both 42 # t.datetime :locked_at 43 44 45 t.timestamps null: false 46 end 47 48 add_index :users, :nickname, unique: true 49 add_index :users, :email, unique: true 50 add_index :users, :reset_password_token, unique: true 51 # add_index :users, :confirmation_token, unique: true 52 # add_index :users, :unlock_token, unique: true 53 end 54end
ご有識のある方、ご助力お願いします。
蛇足な記述等あればそちらもご指摘いただけると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/18 12:35
2020/06/18 12:41
2020/06/18 12:56