ruby-on-railsrubypaperclipattachment

validations of size and content type not working in rails 7 using paperclip


I am currently revamping old system which was built in rails 3 and ruby-1.9.3 to rails 7.1.3.2 and ruby-3.3.0. Now in my old app, it was using paperclip for all attachments so I will also be using paperclip in my new app. In my old app, all validations work like content type and size but I am having trouble working with validations in rails 7 which my new app is using.

This is my model code.

class User < ApplicationRecord
  base_url = "/home/muhammadans41/Downloads/paperclip/comments/:id/:style/:basename.:extension"
  base_path = "/home/muhammadans41/Downloads/paperclip/comments/:id/:style/:basename.:extension"
  has_attached_file :avatar, storage: :filesystem, styles: { small: "264x204>", large: "800x800>"}, path: "#{base_path}", url: "#{base_url}"
  validates_attachment_content_type :avatar, content_type: ["image/jpeg", "image/jpg", "application/pdf"]
  validates_attachment_size :avatar, :less_than => 2.megabytes
end

This is my schema

create_table "users", force: :cascade do |t|
  t.string "username"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "avatar_file_name"
  t.integer "avatar_file_size"
  t.string "avatar_content_type"
  t.datetime "avatar_updated_at"
end

This is my form

<%= form_with(model: user) do |form| %>
  <% if user.errors.any? %>
    <div style="color: red">
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
      <ul>
        <% user.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <div>
    <%= form.label :username, style: "display: block" %>
    <%= form.text_field :username %>
  </div>
   <div>
    <%= form.file_field :avatar %>
  </div>
  <div>
    <%= form.submit %>
  </div>
<% end %>

This is my controller

class UsersController < ApplicationController
  before_action :set_user, only: %i[ show edit update destroy ]

  # GET /users or /users.json
  def index
    @users = User.all
  end

  # GET /users/1 or /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users or /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to user_url(@user), notice: "User was successfully created." }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1 or /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1 or /users/1.json
  def destroy
    @user.destroy!

    respond_to do |format|
      format.html { redirect_to users_url, notice: "User was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def user_params
      params.require(:user).permit(:username, :avatar)
    end
end

Problem Explanation

  1. when I upload legit files, it git uploaded. When I upload pdf file, I get this on screen enter image description here
  2. when I upload files other than the provides one's in validation model like png or excel or zip, I get this. enter image description here
  3. And when I upload a legit file extension but with huge size so that validation can fail, I get this enter image description here

4. Ruby 3.3.0 Rails 7.1.3.2 Paperclip 6.1


Solution

  • This looks to me like Paperclip isn’t compatible with the version of Ruby you’re now on—the errors around number of arguments are likely around the changes to Ruby’s named argument syntax.

    Paperclip is now unmaintained, but there’s a maintained fork, kt-paperclip; I’d recommend switching to that and seeing if it solves your issue.