前提・実現したいこと
Ruby on Railsで簡単な写真投稿サイトを作成しております。
新規投稿画面にて写真を選択後、投稿ボタンを押しても、
「Unpermitted parameter: :images」が出てしまい投稿に失敗します。
プレビューにも表示されているように写真の選択はできているはずなのですが、
投稿に失敗する原因がわからないため、ご質問させていただきます。
初心者のため至らない点が多いですがよろしくお願いします。
該当のソースコード
controller
1class PostsController < ApplicationController 2 before_action :authenticate_user!, only: %i[create destroy new edit] 3 before_action :correct_user, only: %i[edit] 4 5 def new 6 @post = current_user.posts.build 7 end 8 9 def create 10 @post = current_user.posts.build(posts_params) 11 if @post.save 12 redirect_to current_user, flash: { success: "投稿しました。" } 13 else 14 render :new 15 end 16 end 17 18 private 19 20 def posts_params 21 params.require(:post).permit( 22 :title, 23 :content, 24 { images: [] }, 25 :tag_list 26 ) 27 end 28 29 def correct_user 30 @post = Post.find(params[:id]) 31 redirect_to(root_url) unless @post.user == current_user 32 end 33end
view
1<%= javascript_include_tag 'profile_js' %> 2<div> 3 <h2 class="font-weight-bold text-center">投稿フォーム</h2> 4 <div class="form-group mt-5"> 5 <%= render "layouts/error_message", object: @post %> 6 <%= form_for(@post, class: "form") do |f| %> 7 8 <div class=text-center> 9 <div> 10 <% if @post.images.present? %> 11 <%= image_tag post.images.url, id: :js_imgPrev, size:"600x450", class:"post-prev img-fluid" %> 12 <% else %> 13 <%= image_tag asset_path("defaultPicture.jpg"), id: :js_imgPrev, size:"300x450", class:"post-prev img-fluid" %> 14 <% end %> 15 </div> 16 <label for="js_presentImg" class="btn btn-warning"><span>画像を選択</span> 17 <%= f.file_field :images, accept: 'image/jpeg,image/png', id: :js_presentImg, autofocus: true, 18 class:"form-control-file", style: "display:none;" %> 19 </label> 20 </div> 21 22 <div class="form-group"> 23 <label for="name">Title</label> 24 <%= f.text_field :title, placeholder: "タイトル", class:"form-control" %> 25 </div> 26 27 <div class="form-group"> 28 <label for="name">説明</label> 29 <%= f.text_area :content, placeholder: "説明", class:"form-control" %> 30 </div> 31 32 <div class="form-group"> 33 <label for="name">タグ</label> 34 <%= f.text_field :tag_list, value: @post.tag_list.join(','), placeholder: "タグ(カンマ区切り)", class: "form-control"%> 35 </div> 36 37 <div class="actions text-center"> 38 <%= f.submit "投稿", class: "btn btn-primary" %> 39 </div> 40 <% end %> 41 </div> 42</div>
model
1class Post < ApplicationRecord 2 belongs_to :user 3 default_scope -> { order(created_at: :desc) } 4 mount_uploaders :images, ImageUploader 5 validates :images, presence: { message: 'を入力してください。' } 6 validates :user_id, presence: true 7 validates :title, presence: true, length: { maximum: 50 } 8end
Log
1Processing by PostsController#create as HTML 2 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"=>"投稿"} 3 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] 4Unpermitted parameter: :images 5 Rendering posts/new.html.erb within layouts/application 6 Rendered layouts/_error_message.html.erb (Duration: 0.2ms | Allocations: 121) 7 Rendered posts/new.html.erb within layouts/application (Duration: 2.9ms | Allocations: 2392) 8[Webpacker] Everything's up-to-date. Nothing to do 9 Rendered layouts/_header.html.erb (Duration: 0.5ms | Allocations: 434) 10 Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5) 11Completed 200 OK in 14ms (Views: 8.9ms | ActiveRecord: 0.2ms | Allocations: 10914) 12
補足情報(FW/ツールのバージョンなど)
Ruby:2.6.5
Rails:6.0.2.1
写真投稿機能:carrierwave
追記
コントローラのposts_paramsメソット内を「 { images: [] } → :images 」と編集した場合、同様のエラーログが表示されました。
Log
1Started POST "/posts" for ::1 at 2020-04-09 00:34:59 +0900 2 (0.1ms) SELECT sqlite_version(*) 3Processing by PostsController#create as HTML 4 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"=>"投稿"} 5 User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] 6Unpermitted parameter: :images 7 Rendering posts/new.html.erb within layouts/application 8 Rendered layouts/_error_message.html.erb (Duration: 0.3ms | Allocations: 121) 9 Rendered posts/new.html.erb within layouts/application (Duration: 7.2ms | Allocations: 2400) 10[Webpacker] Everything's up-to-date. Nothing to do 11 Rendered layouts/_header.html.erb (Duration: 0.8ms | Allocations: 500) 12 Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5) 13Completed 200 OK in 66ms (Views: 29.1ms | ActiveRecord: 0.5ms | Allocations: 36059)
回答2件
あなたの回答
tips
プレビュー