質問するログイン新規登録

質問編集履歴

2

中間モデルを載せました。

2020/05/02 15:28

投稿

rr2
rr2

スコア3

title CHANGED
File without changes
body CHANGED
@@ -45,8 +45,7 @@
45
45
  ```Ruby
46
46
  Rails.application.routes.draw do
47
47
  devise_for :users
48
- get 'records/index'
49
- root 'welcome#index'
48
+ root 'records#index'
50
49
  resources :users, only: [:edit, :update]
51
50
  resources :families, only: [:new, :create]
52
51
  end
@@ -60,16 +59,6 @@
60
59
  # before_action :authenticate_user!
61
60
  before_action :configure_permitted_parameters, if: :devise_controller?
62
61
 
63
-
64
- def after_sign_in_path_for(resource)
65
- records_index_path # ログイン後に遷移するpathを設定
66
- end
67
-
68
- def after_sign_out_path_for(resource)
69
- new_user_session_path # ログアウト後に遷移するpathを設定
70
- end
71
-
72
-
73
62
  protected
74
63
 
75
64
  def configure_permitted_parameters
@@ -102,4 +91,12 @@
102
91
  has_many :family_users
103
92
  has_many :families, through: :family_users
104
93
  end
94
+ ```
95
+
96
+ 中間モデル family_user.rb
97
+ ```Ruby
98
+ class FamilyUser < ApplicationRecord
99
+ belongs_to :family
100
+ belongs_to :user
101
+ end
105
102
  ```

1

FamilyモデルとUserモデルを載せました。

2020/05/02 15:28

投稿

rr2
rr2

スコア3

title CHANGED
File without changes
body CHANGED
@@ -76,4 +76,30 @@
76
76
  devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
77
77
  end
78
78
  end
79
+ ```
80
+ Familyモデル
81
+
82
+ ```Ruby
83
+ class Family < ApplicationRecord
84
+ def change
85
+ create_table :families do |t|
86
+ has_many :family_users
87
+ has_many :users, through: :family_users
88
+ validates :name, presence: true, uniqueness: true
89
+ end
90
+ end
91
+ end
92
+ ```
93
+ Userモデル
94
+ ```Ruby
95
+ class User < ApplicationRecord
96
+ # Include default devise modules. Others available are:
97
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
98
+ devise :database_authenticatable, :registerable,
99
+ :recoverable, :rememberable, :validatable
100
+ validates :name, presence: true, uniqueness: true
101
+
102
+ has_many :family_users
103
+ has_many :families, through: :family_users
104
+ end
79
105
  ```