gpt4 book ai didi

ruby-on-rails - Cloudinary 上传有效但无法检索图像

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:03 24 4
gpt4 key购买 nike

我正在使用 Ruby on Rails 开发一个应用程序,但我确实在努力解决以下问题。

我已经设置 Cloudinary http://cloudinary.com/documentation/rails_integration还有附件https://github.com/assembler/attachinary对于用户使用 Rails 应用程序上传个人资料图像,我还希望能够在 Rails 应用程序中加载这些图像以显示用户的个人资料图像。

我在 user.rb 模型文件中设置了以下代码

has_attachment :avatar, :accept => [:jpg, :png, :gif]

User 的编辑页面位于我的应用程序的 Devise View 下 - edit.html.erb。以下表格是我用来上传用户头像/个人资料图片的表格:

  <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :post }) do |f| %>
<%= cl_image_upload_tag(:avatar) %>
<%= f.button :submit %>
<% end %>

在同一个 View 文件中,我使用以下代码来检索个人资料图片:

 <div class="profile-image" align="center">
<% if @user.avatar.present? %>
<img src="<%= cloudinary_url(@user.avatar.path) %>" width="180px" height="180px" alt=""/>
<% else %>
<img src="<%= asset_path "person.png" %>" width="180px" height="180px" alt=""/>
<% end %>

</div>

图片上传到 Cloudinary 后,它会被赋予一个随机文件名,例如:ecwc8x4ult960jzk8dp0

我上传的图片是否正确以及如何检索它们?

最佳答案

在表单中尝试:<%= user.attachinary_file_field :avatar %> .

您应该能够像这样通过模型检索它们:<%= image_tag user.avatar.url if user.avatar? %> ,或者就像附件文档中所说的那样:<%= cl_image_tag(@user.avatar.path, { size: '180x180', crop: :thumb, gravity: :face }) %> .省略 <img>标记,我建议不要使用内联 css。

如果通过 HTTP 而不是 HTTPS 提供图像,您可能会遇到 400 错误。要解决这个问题,您应该添加 secure: true到 cloudinary.yml 或添加 :secure => true到图片标签。

我不能写评论...有没有错误,你能发布错误日志吗?

关于ruby-on-rails - Cloudinary 上传有效但无法检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28517137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com