Ami*_*mie 4 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 %>
Run Code Online (Sandbox Code Playgroud)
events/index.html.erb 我尝试用
<% if event.event_image.attached? %>
<%= image_tag event.event_image %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
错误:无法将图像解析为 URL:未定义方法 `attachment_url' for :0x00007fca5dcef8b8>
<% if event.event_image.attached? %>
<%= image_tag url_for(event.event_image) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
错误:类的未定义方法`attachment_path':0x00007fca5e0109c0>:0x00007fca5dacefe8>
我已经确认我的数据库中存在 active_storage_attachments 和 active_storage_blobs 并且附件保存在那里
任何建议将不胜感激。从我所有的谷歌搜索来看,这似乎应该有效
使用 Spree 时,您应该在url_for方法前面加上main_app.url_for::
image_path(main_app.url_for(event.event_image))
Run Code Online (Sandbox Code Playgroud)
这对 Spree v3.6.5 和 v3.6.6 有效,其他版本不确定。