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

質問編集履歴

2

マークダウン記法に修正

2020/04/11 07:55

投稿

HSRRRJSJQP
HSRRRJSJQP

スコア24

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,9 @@
5
5
  お手数おかけしますがご指摘宜しくお願いいたします。
6
6
 
7
7
  ##エラーメッセージなど
8
+
9
+ ```
10
+
8
11
  ・ビューにはエラ〜メッセージは表示されておりません。
9
12
  コントローラーでflaseの時はnewへrenderするようにしているのでfalse時の条件分岐が作用している状態です。
10
13
  ・ターミナルには下記出力がされています。
@@ -23,6 +26,10 @@
23
26
  Rendered posts/new.html.haml within layouts/application (22.7ms)
24
27
  Completed 200 OK in 101ms (Views: 84.4ms | ActiveRecord: 2.2ms)
25
28
 
29
+ ```
30
+
31
+
32
+
26
33
  ##やったこと
27
34
  ①binding.pryで取得データの確認
28
35
  ②ROLLBACKの箇所にuser_idの記載があったのでアソシエーションの確認

1

マークダウン記法に修正.コントローラー追記

2020/04/11 07:55

投稿

HSRRRJSJQP
HSRRRJSJQP

スコア24

title CHANGED
File without changes
body CHANGED
@@ -23,13 +23,14 @@
23
23
  Rendered posts/new.html.haml within layouts/application (22.7ms)
24
24
  Completed 200 OK in 101ms (Views: 84.4ms | ActiveRecord: 2.2ms)
25
25
 
26
- ###やったこと
26
+ ##やったこと
27
27
  ①binding.pryで取得データの確認
28
28
  ②ROLLBACKの箇所にuser_idの記載があったのでアソシエーションの確認
29
29
 
30
30
  ###検証
31
+ ```
31
32
  ①Post.new(post_params)やpost_paramsにデータが入っていることを確認。
32
- Post.new(post_params)
33
+ Post.new(post_params)
33
34
  User Load (14.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
34
35
  ↳ app/controllers/posts_controller.rb:37
35
36
  => #<Post:0x00007fed558b90f8
@@ -43,8 +44,8 @@
43
44
  user_id: 1,
44
45
  image: nil,
45
46
  category_id: nil>
46
- ・@post.save!を行うも「NoMethodError: undefined method `save!' for nil:NilClass」
47
+ ・@post.save!を行うも「NoMethodError: undefined method `save!' for nil:NilClass」```
47
- ・ストロングパラメータの記載は誤りなし。
48
+
48
49
  ②userとpostのアソシエーションは組めていることを確認
49
50
  (post.rb)
50
51
  class Post < ApplicationRecord
@@ -52,20 +53,27 @@
52
53
  (user.rb)
53
54
  class User < ApplicationRecord
54
55
  has_many :posts
56
+ ```
55
57
 
56
58
  ###仮説
57
59
  指摘箇所を踏まえるとuser_idが原因ではないかと思います。
58
60
  しかし、解決までの糸口を見いだせていない現状です。
59
61
 
62
+ ```
63
+ ###コード
64
+ (posts.controller.rb)
65
+ class PostsController < ApplicationController
60
66
 
61
- ###コード
62
- (posts.controller)
67
+ def index
68
+ @posts = Post.all.limit(3).order("created_at DESC")
69
+ end
70
+
63
71
  def new
64
72
  @post = Post.new
65
73
  end
66
74
 
67
75
  def create
68
- binding.pry
76
+
69
77
  @post = Post.new(post_params)
70
78
  if @post.save
71
79
  redirect_to root_path
@@ -74,7 +82,89 @@
74
82
  end
75
83
  end
76
84
 
85
+ # def show
86
+ # end
87
+
88
+ # def destroy
89
+ # end
90
+
91
+ # def edit
92
+ # end
93
+
94
+ # def update
95
+ # end
96
+
77
97
  def post_params
78
98
  params.require(:post).permit(
79
99
  :title, :pharse, :summery, :image).merge(user_id: current_user.id)
80
- end
100
+ end
101
+
102
+ end
103
+
104
+ (appplication.controller.rb)
105
+ class ApplicationController < ActionController::Base
106
+ before_action :configure_permitted_parameters, if: :devise_controller?
107
+ protect_from_forgery with: :exception
108
+ protected
109
+ def configure_permitted_parameters
110
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :prefecture_id, :city, :birth_year, :birth_month, :birth_day, :age_id, :gender_id])
111
+ end
112
+ end
113
+
114
+ (registrations_controller.rb)
115
+ class Users::RegistrationsController < Devise::RegistrationsController
116
+ before_action :configure_sign_up_params, only: [:create]
117
+ # before_action :configure_account_update_params, only: [:update]
118
+
119
+ def new
120
+ @user = User.new
121
+ end
122
+
123
+ def create
124
+
125
+ @user = User.new(sign_up_params)
126
+ unless @user.valid?
127
+ flash.now[:alert] = @user.errors.full_messages
128
+ render :new and return
129
+ end
130
+ session["devise.regist_data"] = {user: @user.attributes}
131
+ session["devise.regist_data"][:user]["password"] = params[:user][:password]
132
+ @information = @user.build_user_information
133
+ render :new_user_information
134
+ end
135
+
136
+
137
+ def create_user_information
138
+
139
+ @user = User.new(session["devise.regist_data"]["user"])
140
+ @information = UserInformation.new(user_information_params)
141
+ unless @information.valid?
142
+ flash.now[:alert] = @information.errors.full_messages
143
+ render :new_user_information and return
144
+ end
145
+ @user.build_user_information(@information.attributes)
146
+ if @user.save!
147
+ sign_in(:user, @user)
148
+ else
149
+ new_user_registration_path
150
+ end
151
+ end
152
+
153
+ protected
154
+ def configure_sign_up_params
155
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
156
+ end
157
+
158
+
159
+ protected
160
+ def user_information_params
161
+ params.require(:user_information).permit(:prefecture_id, :city, :birth_year, :birth_month, :birth_day, :age_id, :gender_id)
162
+ end
163
+
164
+ def after_sign_up_path_for(resource)
165
+ root_path
166
+ end
167
+ end
168
+
169
+
170
+ ```