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

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

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

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

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Q&A

0回答

977閲覧

Rails devise has_many has_oneの関連付け

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

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

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

0グッド

0クリップ

投稿2021/05/10 11:17

現在Railsアプリを作っている段階で、
Userモデルはdeviseで実装しています。サインアップはSNSログインで実装しており
ユーザーアカウントを編集画面にてprofileモデルとpreferenceモデルの情報を登録する流れで
1対1で紐づけているprofileモデルはconsole上でも登録されているのですが、1対多のpreferenceモデルがコンソール上で登録されない状況です。。
application_controllerのdeviseに対して許可する定義が間違っているのかわからず、もしわかる方がいたら教えていただいても宜しいでしょうか。。

rails

1user.rb 2class User < ApplicationRecord 3 # Include default devise modules. Others available are: 4 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 5 6 #profileモデルとの関連付け 7 has_one :profile, dependent: :destroy 8 #preferenceモデルとの関連付け 9 has_many :preferences, dependent: :destroy 10 #Userモデルを通して,profileの値をDBに保存することができる 11 accepts_nested_attributes_for :profile 12 #Userモデルを通して,preferenceの値をDBに保存することができる 13 accepts_nested_attributes_for :preferences 14 15 devise :database_authenticatable, :registerable, 16 :recoverable, :rememberable, :validatable,:omniauthable, omniauth_providers: [:twitter,:facebook] 17 18 19 #ユーザパスワードなしでもアカウント情報更新可能な処理 20 def update_with_password(params, *options) 21 params.delete(:current_password) 22 23 if params[:password].blank? 24 params.delete(:password) 25 params.delete(:password_confirmation) if params[:password_confirmation].blank? 26 end 27 28 result = update(params, *options) 29 30 clean_up_passwords 31 result 32 end 33 34 def self.find_for_oauth(auth) 35 user = User.where(uid: auth.uid, provider: auth.provider).first 36 37 unless user 38 user = User.create( 39 uid: auth.uid, 40 provider: auth.provider, 41 screen_name: auth.info.nickname, 42 email: User.dummy_email(auth), 43 password: Devise.friendly_token[0, 20] 44 ) 45 end 46 47 user 48 end 49 50 private 51 52 def self.dummy_email(auth) 53 "#{auth.uid}-#{auth.provider}@example.com" 54 end 55 56end 57

rails

1profile.rb 2class Profile < ApplicationRecord 3 belongs_to :user 4 #バリデーション 5 validates :name,length: { maximum: 6 } 6end 7

rails

1preference.rb 2class Preference < ApplicationRecord 3 belongs_to :user 4end 5

rails

1registrations_controller 2 3# frozen_string_literal: true 4 5class Users::RegistrationsController < Devise::RegistrationsController 6 before_action :authenticate_user!, only: [:edit, :update,:destroy] 7 before_action :configure_account_update_params, only: [:update] 8 9 10 # def new 11 # @user = User.new 12 # @profile = @user.build_profile 13 # end 14 15 # def create 16 # super 17 # @user = User.new(profile_params) 18 # @user.build_profile 19 # @user.save 20 # end 21 22# GET /resource/edit 23 def edit 24 @user = User.new(profile_params) 25 @user.preferences.build 26 @user.build_profile 27 end 28 29 # PUT /resource 30 def update 31 super 32 @user = User.new(profile_params) 33 @user.preferences.build 34 @user.build_profile 35 @user.save 36 end 37 38 # DELETE /resource 39 def destroy 40 super 41 end 42 43 44 def configure_sign_up_params 45 devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 46 end 47 48 # If you have extra params to permit, append them to the sanitizer. 49 def configure_account_update_params 50 devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 51 end 52 53 # # The path used after sign up. 54 # def after_sign_up_path_for(resource) 55 # super(resource) 56 # end 57 58 # # The path used after sign up for inactive accounts. 59 # def after_inactive_sign_up_path_for(resource) 60 # super(resource) 61 # end 62 63 private 64 65 def profile_params 66 params.permit(:account_update, keys: [profile_attributes: [:ringsize,:image,:pinkiesize,:birthday,:anivday1,:anivday2,:name],preferences_attributes: [:lovebrand,:lovecolor,:lovematerial,:lovesituation]]) 67 end 68end 69

rails

1application_controller 2class ApplicationController < ActionController::Base 3 before_action :configure_permitted_parameters, if: :devise_controller? 4 add_flash_types :success, :info, :warning, :danger 5 protect_from_forgery with: :exception 6 7 protected 8 def configure_permitted_parameters 9 devise_parameter_sanitizer.permit(:account_update, keys: [profile_attributes: [:ringsize,:image,:pinkiesize,:birthday,:anivday1,:anivday2,:name], preferences_attributes: [:lovebrand,:lovecolor,:lovematerial,:lovesituation]]) 10 end 11 end

rails

1registrations/edit.html 2 3 <h2>ユーザ情報変更画面</h2> 4<% 5=begin%> 6 <%= form_with model: @user,url: user_registration_path,id:"new_user" do |f| %> 7<% 8=end%> 9<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 10<%= render 'partial/error_form', model: f.object %> 11 12 <%= f.fields_for :profile do |f| %> 13 <div class="field"> 14 <%= f.label :name %><br /> 15 <%= f.text_field :name %> 16 </div> 17 <div class="field"> 18 <%= f.label :ringsize %><br /> 19 <%= f.select :ringsize,[["1号",1],["2号",2],["3号",3],["4号",4],["5号",5],["6号",6]] %> 20 </div> 21 <div class="field"> 22 <%= f.label :pinkiesize %><br /> 23 <%= f.select :pinkiesize,[["1号",1],["2号",2],["3号",3],["4号",4],["5号",5],["6号",6]] %> 24 </div> 25 26 27 <%= f.fields_for :preference do |d|%> 28 <div class="field"> 29 <%= d.label :lovebrand %><br /> 30 <%= d.text_field :lovebrand %> 31 </div> 32 <div class="field"> 33 <%= d.label :lovematerial %><br /> 34 <%= d.text_field :lovematerial %> 35 </div> 36 37 <div class="field"> 38 <%= d.label :lovecolor %><br /> 39 <%= d.text_field :lovecolor %> 40 </div> 41 42 43 44 <div class="field"> 45 <%= f.label :birthday %><br /> 46 <%= f.date_field :birthday %> 47 </div> 48 <div class="field"> 49 <%= f.label :anivday1 %><br /> 50 <%= f.date_field :anivday1 %> 51 </div> 52 <div class="field"> 53 <%= f.label :anivday2 %><br /> 54 <%= f.date_field :anivday2 %> 55 </div> 56 57 <%= f.submit "更新する", class: "btn btn-primary" %> 58 <% end %> 59 <% end %> 60 <% end %> 61 62 63<p>ユーザアカウント削除ボタン <%= button_to "退会する", registration_path(resource_name), data: { confirm: "アカウントのデータを消してもいいですか??" }, method: :delete %></p> 64 65<%= link_to "Back", :back %> 66

terminal

1ターミナル 2[2] pry(#<Users::RegistrationsController>)> params 3=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"g6Vy0MwN0OQ6+5OYuSBSGiX67jvcHzHsH1izcSVHlIVr7CWo9F0+LIsgC9GHJzjiO+rgLlaXd62hqJOlxymolg==", "user"=>{"profile_attributes"=>{"name"=>"ナオキ", "ringsize"=>"3", "pinkiesize"=>"3", "preference"=>{"lovebrand"=>"Gucci", "lovematerial"=>"銀", "lovecolor"=>"白"}, "birthday"=>"2021-05-22", "anivday1"=>"2021-05-14", "anivday2"=>"2021-05-13"}}, "commit"=>"更新する", "controller"=>"users/registrations", "action"=>"update"} permitted: false>

schemarb

1ActiveRecord::Schema.define(version: 20210510044849) do 2 3 create_table "preferences", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 4 t.string "lovebrand" 5 t.string "lovecolor" 6 t.string "lovematerial" 7 t.string "lovesituation" 8 t.bigint "user_id" 9 t.datetime "created_at", null: false 10 t.datetime "updated_at", null: false 11 t.index ["user_id"], name: "index_preferences_on_user_id" 12 end 13 14 create_table "profiles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 15 t.integer "ringsize" 16 t.string "image" 17 t.integer "pinkiesize" 18 t.date "birthday" 19 t.date "anivday1" 20 t.date "anivday2" 21 t.bigint "user_id" 22 t.string "name" 23 t.datetime "created_at", null: false 24 t.datetime "updated_at", null: false 25 t.index ["user_id"], name: "index_profiles_on_user_id" 26 end 27 28 create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 29 t.string "email", default: "", null: false 30 t.string "encrypted_password", default: "", null: false 31 t.string "reset_password_token" 32 t.datetime "reset_password_sent_at" 33 t.datetime "remember_created_at" 34 t.datetime "created_at", null: false 35 t.datetime "updated_at", null: false 36 t.string "provider" 37 t.string "uid" 38 t.string "name" 39 t.string "screen_name" 40 t.index ["email"], name: "index_users_on_email", unique: true 41 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 42 end 43 44 add_foreign_key "preferences", "users" 45 add_foreign_key "profiles", "users" 46end

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問