【发布时间】:2015-04-15 12:43:27
【问题描述】:
我正在使用 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]
用户的编辑页面位于我的应用程序的设计视图下 - 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 %>
在同一个视图文件中,我使用以下代码检索个人资料图片:
<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
我是否正确上传了图片以及如何检索它们?
【问题讨论】:
标签: ruby-on-rails ruby devise cloudinary