jjplack
jjplack

Reputation: 123

Rails Carrierwave Unpermitted parameter

that's simple thing at least should be simple bogus me out! I'm trying to just add an upload multiple photos to user but every time it hits to Unpermitted parameter

So the user is a simple user model with devise and a photo model belongs to user

I just want to upload multiple photos with carrierwave to photos model.

but every time this Unpermitted parameter image hits, as you can see the image params is permitted so I don't have any clue why rails still complain about this param so can someone clarify about this?

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'

here is the code!

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

Upvotes: 0

Views: 184

Answers (1)

Fernand
Fernand

Reputation: 1333

Strong Parameters only accept scalar types (String, Symbol, NilClass, etc...).

You passed an array type (Parameters: {"authenticity_token"=>"[FILTERED]", "photo"=>{"image"=>[#<...), therefore change the permit params to an array of scalar.

def photo_params
  params.require(:photo).permit(:image => [],:user_id,:id,)
end

Upvotes: 1

Related Questions