当我上传文件时,我会创建一个 cloudinary url,它完全正常并且可以工作 但是当我执行 @user.update 时,我收到此错误: ActiveSupport::MessageVerifier::InvalidSignature in UsersController#update - 我收到此错误 我的控制器方法有问题吗?
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def search
@search_results = User.search_by_username(params[:query])
end
def update
@user = User.find(params[:id])
if params[:user] && params[:user][:profile_pic_url]
profile_pic_url = retrieve_cloudinary_url(params[:user][:profile_pic_url])
puts profile_pic_url
if profile_pic_url
if @user.update(profile_pic_url: profile_pic_url)
redirect_to @user, notice: 'Profile picture was successfully updated.'
else
flash[:alert] = "Failed to update profile picture. Error: #{user_url_error}"
redirect_to @user
end
else
flash[:alert] = 'Invalid image format. Please upload a valid image.'
redirect_to @user
end
else
flash[:alert] = 'Profile picture URL missing. Please provide a valid URL.'
redirect_to @user
end
end
private
def retrieve_cloudinary_url(uploaded_file)
if uploaded_file.respond_to?(:tempfile) && uploaded_file.content_type.start_with?('image/')
cloudinary_response = Cloudinary::Uploader.upload(uploaded_file.tempfile.path)
cloudinary_url = Cloudinary::Utils.cloudinary_url(cloudinary_response['public_id'])
return cloudinary_url
end
nil
end
def user_url_error
@user.errors[:profile_pic_url].first if @user.errors[:profile_pic_url].any?
end
end
我认为这可能与不包括强参数有关,但是当我使用它时 - 我不断收到 -无效 URL - 的重复错误,因为
params.require(:user).permit(:profile_pic_url)
之后的 profile_pic_url 属性不断返回这种格式的响应
{"profile_pic_url"=>#<ActionDispatch::Http::UploadedFile:0x0aea3a88 @tempfile=#<Tempfile:C:/Users/LENOVO/AppData/Local/Temp/RackMultipart20230822-23140-zog8og.png>, @original_filename="Screenshot 2023-03-05 152821.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[profile_pic_url]\"; filename=\"Screenshot 2023-03-05 152821.png\"\r\nContent-Type: image/png\r\n">}
这是我的整个控制器方法 - 我已经尝试了两种更新功能
# frozen_string_literal: true
# This controller is used to show the user profile page
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def search
@search_results = User.search_by_username(params[:query])
end
# def update
# @user = User.find(params[:id])
# puts "User params -------------------------: #{user_params}"
# if @user.update(user_params)
# # Update successful
# redirect_to @user, notice: 'Profile picture updated.'
# else
# # Update failed
# render plain: 'Failed to update profile picture.'
# end
# end
def update
@user = User.find(params[:id])
if params[:user] && params[:user][:profile_pic_url]
profile_pic_url = retrieve_cloudinary_url(params[:user][:profile_pic_url])
puts profile_pic_url
if profile_pic_url
if @user.update(profile_pic_url: profile_pic_url)
redirect_to @user, notice: 'Profile picture was successfully updated.'
else
flash[:alert] = "Failed to update profile picture. Error: #{user_url_error}"
redirect_to @user
end
else
flash[:alert] = 'Invalid image format. Please upload a valid image.'
redirect_to @user
end
else
flash[:alert] = 'Profile picture URL missing. Please provide a valid URL.'
redirect_to @user
end
end
private
def retrieve_cloudinary_url(uploaded_file)
if uploaded_file.respond_to?(:tempfile) && uploaded_file.content_type.start_with?('image/')
cloudinary_response = Cloudinary::Uploader.upload(uploaded_file.tempfile.path)
cloudinary_url = Cloudinary::Utils.cloudinary_url(cloudinary_response['public_id'])
return cloudinary_url
end
nil
end
def user_url_error
@user.errors[:profile_pic_url].first if @user.errors[:profile_pic_url].any?
end
end
# def user_params
# params.require(:user).permit(:profile_pic_url)
# end
这是我的 users/show.html.erb
<div class="user-profile-container">
<div class="user-header">
<div class="profile-image">
<% if @user.profile_pic_url.attached? %>
<%= cl_image_tag @user.profile_pic_url.key, :width=>150, :crop=>"fill" %>
<% else %>
<%= image_tag 'default_pfp.png', class: "circle-avatar" %>
<% end %>
<% if @user.profile_pic_url.attached? %>
<% puts "Cloudinary Public ID: #{@user.profile_pic_url.key}" %>
<% end %>
</div>
<div class="user-info">
<div class="username">
<span><%= @user.username %></span>
<div style="width: 150px;"></div>
</div>
<div class="user-actions">
<%= link_to "Edit Profile", edit_user_registration_path, class: "gray-button" %>
<button class="gray-button">View Archive</button>
<%= image_tag "icons/settings.jpg", class: "settings-icon" %>
</div>
</div>
</div>
<div class="user-stats-container">
<div class="user-stats">
<p>0 posts</p>
<p><%= @user.followers.count %> followers</p>
<p><%= @user.following.count %> following</p>
</div>
</div>
<% if current_user != @user %>
<% if current_user.following.include?(@user) %>
<%= button_to "Unfollow", user_follow_path(@user), method: :delete %>
<% else %>
<%= button_to "Follow", user_follow_path(@user), method: :post %>
<% end %>
<% end %>
</div>
<%= form_for @user, url: user_path(@user), method: :patch, html: { multipart: true } do |f| %>
<%= f.label :profile_pic_url, 'Profile Picture' %>
<%= f.file_field :profile_pic_url %>
<%= f.submit 'Update Profile' %>
<% end %>
此错误通常与 Rails 用于防止跨站点请求伪造 (CSRF) 攻击的真实性令牌验证有关。您可以在其他帖子中阅读有关解决方案的信息,例如: ActiveSupport::MessageVerifier::InvalidSignature: ActiveSupport::MessageVerifier::InvalidSignature Rails 5 中的错误