質問編集履歴
1
updateアクションないを変更しました!
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
permitted: false の解決法
|
body
CHANGED
@@ -1,27 +1,60 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
タイトルに記載しました、 permitted:falseのエラーでつまずいています。
|
4
|
-
|
4
|
+
調べてみると、ストロングパラメータの記載に問題があるのだということが分かったのですが、
|
5
|
+
下記に記載している通り、users_controller.rbの下部(private)にparams.requireを追記したのですが変わりません。
|
5
6
|
|
6
|
-
ユーザーのマイページの内容の更新を行おうとするところで、wrong number of arguments (given 4, expected 1)のエラーが出ます。
|
7
|
-
|
8
|
-
呼び出し側とメソッド側の引数のが合わないのかなと思ったのですが、どうしたらいいか探し出せませんでした。
|
9
|
-
|
10
|
-
ご教授、よろしくお願いいたします!
|
7
|
+
ご教授のほど、よろしくお願いいたします!!
|
11
8
|
### 発生している問題・エラーメッセージ
|
12
|
-
|
9
|
+
permitted: false
|
13
10
|
|
14
11
|
### 該当のソースコード
|
15
12
|
|
16
|
-
|
13
|
+
```エラ〜メッセージ
|
14
|
+
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"0nEBSbJ5+7+dhMOi8avmT7lpopkdZgqIFGreJnWP2alMQ+dMH/TKIMi8y+YM/ZXf/JI8WQBJJ3AIKesxe0KTpA==", "name"=>"べべべ", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007f86e0469538 @tempfile=#<Tempfile:/var/folders/4j/366dq7y542n_xt2k1d5bq32w0000gn/T/RackMultipart20201014-2391-13b8a4d.png>, @original_filename="スクリーンショット 2020-10-02 18.16.34.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"\xE3\x82\xB9\xE3\x82\xAF\xE3\x83\xAA\xE3\x83\xBC\xE3\x83\xB3\xE3\x82\xB7\xE3\x83\xA7\xE3\x83\x83\xE3\x83\x88 2020-10-02 18.16.34.png\"\r\nContent-Type: image/png\r\n">, "bgimage"=>#<ActionDispatch::Http::UploadedFile:0x00007f86e0490e58 @tempfile=#<Tempfile:/var/folders/4j/366dq7y542n_xt2k1d5bq32w0000gn/T/RackMultipart20201014-2391-1exzwwp.jpg>, @original_filename="sean-o-KMn4VEeEPR8-unsplashのコピー.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"bgimage\"; filename=\"sean-o-KMn4VEeEPR8-unsplash\xE3\x81\xAE\xE3\x82\xB3\xE3\x83\x92\xE3\x82\x9A\xE3\x83\xBC.jpg\"\r\nContent-Type: image/jpeg\r\n">, "intro"=>"かか", "controller"=>"users", "action"=>"update", "id"=>"3"} permitted: false>
|
15
|
+
```
|
16
|
+
ソースコード
|
17
|
+
|
18
|
+
```rails
|
19
|
+
class UsersController < ApplicationController
|
20
|
+
|
21
|
+
before_action :set_user, only: [:show,:update,:edit,:delete]
|
22
|
+
def new
|
23
|
+
@user = User.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def create
|
27
|
+
@user = User.new(name: params[:name], email: params[:email], password: params[:password], image: "emma-smith-NS_DMD4rEjk-unsplash.jpg", bgimage: "自然壁画.jpg") # 実装は終わっていないことに注意!
|
28
|
+
# binding.pry
|
29
|
+
if @user.save
|
30
|
+
session[:user_id] = @user.id
|
31
|
+
flash[:success] = "Welcome to this app!"
|
32
|
+
redirect_to("/home/about")
|
33
|
+
else
|
34
|
+
flash[:notice] = "Your email is already existed"
|
35
|
+
render 'users/new'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def show
|
40
|
+
@users = User.find_by(id: params[:id])
|
41
|
+
# binding.pry
|
42
|
+
end
|
43
|
+
|
44
|
+
def edit
|
45
|
+
@users = User.find_by(id: params[:id])
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def update
|
17
50
|
|
18
|
-
@users = User.find_by(id:
|
51
|
+
@users = User.find_by(id: params[:id])
|
19
|
-
|
20
|
-
if @users.update(
|
21
52
|
@users.name = params[:name],
|
22
53
|
@users.image = params[:image],
|
23
54
|
@users.bgimage = params[:bgimage],
|
24
|
-
@users.intro = params[:intro]
|
55
|
+
@users.intro = params[:intro]
|
56
|
+
|
57
|
+
if @users.save
|
25
58
|
# binding.pry
|
26
59
|
flash[:notice] = "編集しました"
|
27
60
|
redirect_to("/users/#{@users.id}/show")
|
@@ -29,16 +62,57 @@
|
|
29
62
|
render("/home/about")
|
30
63
|
|
31
64
|
end
|
65
|
+
|
32
|
-
|
66
|
+
end
|
33
67
|
|
68
|
+
def login_form
|
69
|
+
end
|
70
|
+
|
71
|
+
def login
|
72
|
+
@user = User.find_by(email: params[:email])
|
73
|
+
if @user && @user.authenticate(params[:password])
|
74
|
+
session[:user_id] = @user.id
|
75
|
+
flash[:success] = "welcome Back"
|
76
|
+
redirect_to ("/home/about")
|
77
|
+
else
|
78
|
+
@error_message = "Wrong email or password"
|
79
|
+
@email = params[:email]
|
80
|
+
@password = params[:password]
|
81
|
+
render("/users/login_form")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def logout
|
86
|
+
session[:user_id] = nil
|
87
|
+
flash[:notice] = "Logout"
|
88
|
+
redirect_to("/home/about")
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
|
95
|
+
def user_params
|
96
|
+
params.require(:user).permit(:name, :image, :bgimage, :intro)
|
97
|
+
end
|
98
|
+
|
99
|
+
def set_user
|
100
|
+
#特定データの取得
|
101
|
+
@user = User.find_by(params[:id])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
```erb
|
34
108
|
<div class="edit_profile-wrapper">
|
35
109
|
<%= @users.name %>
|
36
|
-
<%= form_tag("/users/#{@
|
110
|
+
<%= form_tag("/users/#{@users.id}/update", {multipart: true}) do %>
|
37
111
|
|
38
112
|
<div class="edit_profile-contents">
|
39
113
|
<p>名前</p>
|
40
114
|
<label for="name"></label>
|
41
|
-
<input type="text" name="name" value="<%=@
|
115
|
+
<input type="text" name="name" value="<%=@users.name%>" required>
|
42
116
|
</div>
|
43
117
|
<div class="edit_profile-contents">
|
44
118
|
<p>メインアイコン</p>
|
@@ -52,12 +126,13 @@
|
|
52
126
|
</div>
|
53
127
|
<div class="edit_profile-contents">
|
54
128
|
<p>自己紹介文</p>
|
55
|
-
<textarea class="input-text" type="text" placeholder=" enter your introduction!" name="intro"></textarea>
|
129
|
+
<textarea class="input-text" type="text" placeholder=" enter your introduction!" name="intro" value="<%=@users.intro%>"></textarea>
|
56
130
|
</div>
|
57
131
|
<input type="submit" value="submit" class="btn btn-border-shadow btn-border-shadow--color2">
|
58
132
|
|
59
133
|
<%end%>
|
60
134
|
</div>
|
135
|
+
```
|
61
136
|
|
62
137
|
### 試したこと
|
63
138
|
|