我目前正在将用 Rails 3 和 ruby-1.9.3 构建的旧系统改造为 Rails 7.1.3.2 和 ruby-3.3.0。现在在我的旧应用程序中,它对所有附件使用回形针,因此我也将在我的新应用程序中使用回形针。在我的旧应用程序中,所有验证都像内容类型和大小一样工作,但我在新应用程序正在使用的 Rails 7 中使用验证时遇到了麻烦。
这是我的模型代码。
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
这是我的架构
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
这是我的表格
<%= 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 %>
这是我的控制器
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
问题解释
4. Ruby 3.3.0 Rails 7.1.3.2 回形针 6.1
在我看来,Paperclip 与您现在使用的 Ruby 版本不兼容——参数数量的错误很可能与 Ruby 命名参数语法的更改有关。
Paperclip 现在无人维护,但有一个维护的叉子,kt-paperclip;我建议切换到该方法,看看它是否可以解决您的问题。