【发布时间】:2021-09-25 22:48:16
【问题描述】:
这很简单,至少应该是简单的假冒我! 我正在尝试向用户添加上传多张照片,但每次都达到 Unpermitted 参数
所以用户是一个带有设计的简单用户模型,照片模型属于用户
我只想用carrierwave上传多张照片到照片模型。
但是每次出现这个 Unpermitted 参数图像时,你可以看到图像参数是允许的,所以我不知道为什么 rails 仍然抱怨这个参数 那么有人可以澄清一下吗?
Started POST "/photos" for ::1 at 2021-09-25 19:28:15 -0300
Processing by PhotosController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "photo"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x00007facf81dc278 @tempfile=#<Tempfile:/tmp/RackMultipart20210925-7943-117n8yk.jpg>, @original_filename="3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image][]\"; filename=\"3.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Criar Photo"}
Unpermitted parameter: :image
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/photos_controller.rb:19:in `create'
这是代码!
class Photo < ApplicationRecord
belongs_to :user
mount_uploader :image, ImageUploader
end
class PhotosController < ApplicationController
before_action :authenticate_user!, only: %i[new create edit update destroy]
before_action :set_photo, only: %i[show edit update destroy]
def index
@photos = current_user.photos
end
def new
#@photo = current_user.photos.build
@photo = Photo.new
end
def create
@photo = Photo.new(photo_params)
@photo.user_id = current_user.id
respond_to do |format |
if @photo.save
format.html {
redirect_to photos_path, notice: "asset was successfully created."
}
else
format.html {
render :new,
status: unprocessable_entity
}
end
end
end
private
def set_photo
@photo = Photo.friendly.find(params[:id])
end
def photo_params
params.require(:photo).permit(:image,:user_id,:id,)
end
def pagy_get_vars(collection, vars) {
count: collection.count,
page: params['page'],
items: vars[:items] || 25
}
end
end
【问题讨论】:
标签: ruby-on-rails ruby carrierwave