尝试从Rails 5.2中的ActiveStorage显示图像时出现错误

时间:2018-11-07 21:24:44

标签: ruby-on-rails spree rails-activestorage

我正在使用Rails 5.2和Ruby 2.3.7开发Rails应用,并希望使用Active Storage将图像附加到我的Event对象。

这是我采取的主要步骤

在config / environments / development.rb中,我确认:config.active_storage.service =:local

文件

event.rb我添加了这一行: has_one_attached:event_image

events_controller.rb我在event_params中将:event_image属性列入白名单

events / _form.html.erb我设置了一个form_for来上传图片

<%= f.label :event_images %>
<%= f.file_field :event_image %>

events / index.html.erb我尝试使用显示图像

<% if event.event_image.attached? %>
<%= image_tag event.event_image %>
<% end %>

错误:无法将图像解析为URL::0x00007fca5dcef8b8的未定义方法“ attachment_url”>

<% if event.event_image.attached? %>
<%= image_tag url_for(event.event_image) %>
<% end %>

错误:Class:0x00007fca5e0109c0>:0x00007fca5dacefe8>的未定义方法`attachment_path'

我有 确认我的数据库中存在active_storage_attachments和active_storage_blobs并将附件保存在其中

任何建议将不胜感激。从我所有的谷歌搜索看来,这应该可行

3 个答案:

答案 0 :(得分:2)

使用Spree时,应在url_for方法前加上以下名称:main_app.url_for

image_path(main_app.url_for(event.event_image))

这在Spree v3.6.5和v3.6.6中有效,不确定其他版本。

答案 1 :(得分:0)

有关此问题的进一步更新。我在自己的应用程序中重复了相同的步骤,效果很好。仅当使用使用Spree的应用程序时才会发生这种情况

答案 2 :(得分:0)

image_path(main_app.url_for(event.event_image)) 

在我的spree 4.0扩展程序上它对我有用。

相关问题