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

質問編集履歴

10

修正

2020/04/11 11:11

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -2,17 +2,13 @@
2
2
 
3
3
  Ruby on Railsで簡単な写真投稿サイトを作成しております。
4
4
 
5
- 以下の写真のような新規投稿画面にて写真を選択後、投稿ボタンを押しても、
5
+ 新規投稿画面にて写真を選択後、投稿ボタンを押しても、
6
- 写真を入力してくださいとエラーメッセージが出てしまいます。
6
+ Unpermitted parameter: :images」が出てしまい投稿に失敗します。
7
7
 
8
8
  プレビューにも表示されているように写真の選択はできているはずなのですが、
9
9
  投稿に失敗する原因がわからないため、ご質問させていただきます。
10
10
  初心者のため至らない点が多いですがよろしくお願いします。
11
11
 
12
-
13
- ![イメージ説明](f12c93ccb1b6a6c38a4e994d51c328ab.png)
14
-
15
-
16
12
  ### 該当のソースコード
17
13
 
18
14
  ```controller
@@ -20,29 +16,10 @@
20
16
  before_action :authenticate_user!, only: %i[create destroy new edit]
21
17
  before_action :correct_user, only: %i[edit]
22
18
 
23
- def index
24
- @q = Post.ransack(params[:q])
25
- @posts = if @tag_name = params[:tag_name]
26
- Post.tag_search(params[:tag_name]).page(params[:page]).per(PER)
27
- elsif params[:q]
28
- @q.result.order('created_at DESC').page(params[:page]).per(PER)
29
- else
30
- Post.page(params[:page]).per(PER)
31
- end
32
- end
33
-
34
19
  def new
35
20
  @post = current_user.posts.build
36
21
  end
37
22
 
38
- def show
39
- @post = Post.find(params[:id])
40
- current_user&.footprints&.create(post_id: @post.id)
41
- @like = Like.new
42
- @comment = Comment.new
43
- @comments = @post.comments
44
- end
45
-
46
23
  def create
47
24
  @post = current_user.posts.build(posts_params)
48
25
  if @post.save
@@ -52,27 +29,6 @@
52
29
  end
53
30
  end
54
31
 
55
- def edit
56
- p params[:id]
57
- @post = Post.find(params[:id])
58
- end
59
-
60
- def update
61
- @post = Post.find(params[:id])
62
- if @post.update(posts_params)
63
- redirect_to root_path
64
- else
65
- render :edit
66
- end
67
- end
68
-
69
- def destroy
70
- @post = Post.find(params[:id])
71
- @post.destroy
72
- flash.now[:success] = "投稿を削除しました。"
73
- end
74
-
75
-
76
32
  private
77
33
 
78
34
  def posts_params
@@ -143,34 +99,8 @@
143
99
  validates :images, presence: { message: 'を入力してください。' }
144
100
  validates :user_id, presence: true
145
101
  validates :title, presence: true, length: { maximum: 50 }
146
- # like関係
147
- has_many :likes, dependent: :destroy
148
- has_many :liked_users, through: :likes, source: :user
149
- # comment
150
- has_many :comments, dependent: :destroy
151
- # tag
152
- acts_as_taggable
153
-
154
- def self.search(search)
155
- where(['title LIKE ?', "%#{search}%"])
156
- end
157
-
158
- def self.tag_search(tag_name)
159
- tagged_with(tag_name.to_s)
160
- end
161
102
  end
162
103
  ```
163
- ```DB
164
- create_table "posts", force: :cascade do |t|
165
- t.text "content"
166
- t.integer "user_id"
167
- t.datetime "created_at", precision: 6, null: false
168
- t.datetime "updated_at", precision: 6, null: false
169
- t.string "images"
170
- t.string "title"
171
- t.index ["user_id"], name: "index_posts_on_user_id"
172
- end
173
- ```
174
104
 
175
105
  ```Log
176
106
  Processing by PostsController#create as HTML

9

修正

2020/04/11 11:11

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -140,7 +140,7 @@
140
140
  belongs_to :user
141
141
  default_scope -> { order(created_at: :desc) }
142
142
  mount_uploaders :images, ImageUploader
143
- validates :images, presence: { message: 'が選択されていません' }
143
+ validates :images, presence: { message: 'を入力しくださ' }
144
144
  validates :user_id, presence: true
145
145
  validates :title, presence: true, length: { maximum: 50 }
146
146
  # like関係

8

追記

2020/04/08 15:40

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -135,6 +135,31 @@
135
135
  </div>
136
136
  </div>
137
137
  ```
138
+ ```model
139
+ class Post < ApplicationRecord
140
+ belongs_to :user
141
+ default_scope -> { order(created_at: :desc) }
142
+ mount_uploaders :images, ImageUploader
143
+ validates :images, presence: { message: 'が選択されていません' }
144
+ validates :user_id, presence: true
145
+ validates :title, presence: true, length: { maximum: 50 }
146
+ # like関係
147
+ has_many :likes, dependent: :destroy
148
+ has_many :liked_users, through: :likes, source: :user
149
+ # comment
150
+ has_many :comments, dependent: :destroy
151
+ # tag
152
+ acts_as_taggable
153
+
154
+ def self.search(search)
155
+ where(['title LIKE ?', "%#{search}%"])
156
+ end
157
+
158
+ def self.tag_search(tag_name)
159
+ tagged_with(tag_name.to_s)
160
+ end
161
+ end
162
+ ```
138
163
  ```DB
139
164
  create_table "posts", force: :cascade do |t|
140
165
  t.text "content"
@@ -160,7 +185,6 @@
160
185
  Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
161
186
  Completed 200 OK in 14ms (Views: 8.9ms | ActiveRecord: 0.2ms | Allocations: 10914)
162
187
 
163
-
164
188
  ```
165
189
 
166
190
  ### 補足情報(FW/ツールのバージョンなど)

7

修正

2020/04/08 15:38

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -173,8 +173,17 @@
173
173
 
174
174
  コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、同様のエラーログが表示されました。
175
175
  ```Log
176
+ Started POST "/posts" for ::1 at 2020-04-09 00:34:59 +0900
177
+ (0.1ms) SELECT sqlite_version(*)
176
178
  Processing by PostsController#create as HTML
177
- Parameters: {"authenticity_token"=>"RvtJFOLqX+KnzLHdCme5e5HTjIw4kJvzEeyMUijBAnBVU/fNcKPPOXX+NczpcP4QEJv4TsIVgEgLKWaHdchjZw==", "post"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fd71ac7cbf0 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200409-85994-9q4fxi.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">], "title"=>"あ", "content"=>"あ", "tag_list"=>"あ"}, "commit"=>"投稿"}
178
- User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
179
+ Parameters: {"authenticity_token"=>"9Kk54+zTJ83/yISpcCZBXB46Qy5dBMoHMQwiNtGLAN9VaC2E444uhX9xNRJ2efLN9cGpBwSIoJdhjYyMCVDgYQ==", "post"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fd720081408 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200409-85994-885wsk.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">], "title"=>"あ", "content"=>"あ", "tag_list"=>"あ"}, "commit"=>"投稿"}
180
+ User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
179
181
  Unpermitted parameter: :images
182
+ Rendering posts/new.html.erb within layouts/application
183
+ Rendered layouts/_error_message.html.erb (Duration: 0.3ms | Allocations: 121)
184
+ Rendered posts/new.html.erb within layouts/application (Duration: 7.2ms | Allocations: 2400)
185
+ [Webpacker] Everything's up-to-date. Nothing to do
186
+ Rendered layouts/_header.html.erb (Duration: 0.8ms | Allocations: 500)
187
+ Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
188
+ Completed 200 OK in 66ms (Views: 29.1ms | ActiveRecord: 0.5ms | Allocations: 36059)
180
189
  ```

6

修正

2020/04/08 15:36

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -171,6 +171,10 @@
171
171
 
172
172
  ### 追記
173
173
 
174
- コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下写真のようなエラー画面に遷移してし
175
-
176
- ![![イメージ説明](d7ef91db3e0bc06c93dc41f43f970981.png)](c42f47ff7d09456fec27dbcbad257683.png)
174
+ コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、同様のエラーログが表示されした
175
+ ```Log
176
+ Processing by PostsController#create as HTML
177
+ Parameters: {"authenticity_token"=>"RvtJFOLqX+KnzLHdCme5e5HTjIw4kJvzEeyMUijBAnBVU/fNcKPPOXX+NczpcP4QEJv4TsIVgEgLKWaHdchjZw==", "post"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fd71ac7cbf0 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200409-85994-9q4fxi.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">], "title"=>"あ", "content"=>"あ", "tag_list"=>"あ"}, "commit"=>"投稿"}
178
+ User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
179
+ Unpermitted parameter: :images
180
+ ```

5

追記 編集

2020/04/08 15:33

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -171,6 +171,6 @@
171
171
 
172
172
  ### 追記
173
173
 
174
- コントローラのposts_params内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下の写真のようなエラー画面に遷移してします。
174
+ コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下の写真のようなエラー画面に遷移してします。
175
175
 
176
176
  ![![イメージ説明](d7ef91db3e0bc06c93dc41f43f970981.png)](c42f47ff7d09456fec27dbcbad257683.png)

4

追記

2020/04/08 11:43

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -169,4 +169,8 @@
169
169
  Rails:6.0.2.1
170
170
  写真投稿機能:carrierwave
171
171
 
172
- ### 追記
172
+ ### 追記
173
+
174
+ コントローラの「posts_params」内を「 { images: [] } → :images 」と編集した場合、投稿ボタンを押下時に以下の写真のようなエラー画面に遷移してします。
175
+
176
+ ![![イメージ説明](d7ef91db3e0bc06c93dc41f43f970981.png)](c42f47ff7d09456fec27dbcbad257683.png)

3

追記

2020/04/08 11:40

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -167,4 +167,6 @@
167
167
 
168
168
  Ruby:2.6.5
169
169
  Rails:6.0.2.1
170
- 写真投稿機能:carrierwave
170
+ 写真投稿機能:carrierwave
171
+
172
+ ### 追記

2

誤字

2020/04/08 11:33

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
File without changes
body CHANGED
@@ -147,7 +147,22 @@
147
147
  end
148
148
  ```
149
149
 
150
+ ```Log
151
+ Processing by PostsController#create as HTML
152
+ Parameters: {"authenticity_token"=>"JNyHLThmTh3Fh1seMb9p2f5x2s5MmPaIieMCqfAG706P061Rxgukgnk6p3AyKg2LFmYFa9FTFdRy6oEthU+CoQ==", "post"=>{"images"=>#<ActionDispatch::Http::UploadedFile:0x00007f942c706df0 @tempfile=#<Tempfile:/var/folders/lh/yyn1dsls4r31qfjwv5thnpnw0000gn/T/RackMultipart20200408-84512-bxsvru.jpg>, @original_filename="top.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images]\"; filename=\"top.jpg\"\r\nContent-Type: image/jpeg\r\n">, "title"=>"あ", "content"=>"あ\r\n", "tag_list"=>"あ"}, "commit"=>"投稿"}
153
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
154
+ Unpermitted parameter: :images
155
+ Rendering posts/new.html.erb within layouts/application
156
+ Rendered layouts/_error_message.html.erb (Duration: 0.2ms | Allocations: 121)
157
+ Rendered posts/new.html.erb within layouts/application (Duration: 2.9ms | Allocations: 2392)
158
+ [Webpacker] Everything's up-to-date. Nothing to do
159
+ Rendered layouts/_header.html.erb (Duration: 0.5ms | Allocations: 434)
160
+ Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
161
+ Completed 200 OK in 14ms (Views: 8.9ms | ActiveRecord: 0.2ms | Allocations: 10914)
150
162
 
163
+
164
+ ```
165
+
151
166
  ### 補足情報(FW/ツールのバージョンなど)
152
167
 
153
168
  Ruby:2.6.5

1

誤字

2020/04/07 16:10

投稿

Kazu_p8619
Kazu_p8619

スコア9

title CHANGED
@@ -1,1 +1,1 @@
1
- [Ruby on Rails 6]写真投稿に失敗してしまう。
1
+ [Ruby on Rails 6]写真投稿に失敗してしまう。
body CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  Ruby on Railsで簡単な写真投稿サイトを作成しております。
4
4
 
5
- 以下の写真のような新規投稿画面にて写真を選択投稿ボタンを押しても、
5
+ 以下の写真のような新規投稿画面にて写真を選択後、投稿ボタンを押しても、
6
6
  「写真を入力してください」とエラーメッセージが出てしまいます。
7
7
 
8
+ プレビューにも表示されているように写真の選択はできているはずなのですが、
8
- 様々な方法で試行してみたので原因がわからないため、ご質問させていただきます。
9
+ 投稿に失敗原因がわからないため、ご質問させていただきます。
10
+ 初心者のため至らない点が多いですがよろしくお願いします。
9
11
 
10
12
 
11
13
  ![イメージ説明](f12c93ccb1b6a6c38a4e994d51c328ab.png)