我需要帮助编码显示用户加载图像的数组。我搜索了论坛和教程,我准备承认我被卡住了!我可以看到文件加载到数据库中但我无法访问它们。
感谢您的帮助。
我正在使用rails 5
和ruby 2.3.1p112
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
gem 'sqlite3'
我有:
class Micropost < ApplicationRecord
belongs_to :user
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 200 }
mount_uploaders :pictures, PictureUploader
end
class MicropostController < ApplicationController
.
.
def create
@micropost = current_user.microposts.build(micropost_params)
.
.
params.require(:micropost).permit(:content, {pictures: [ ]})
我的表格:
<%= f.file_field :pictures, multiple: true, accept: 'image/jpeg,image/gif,image/png' %>
我的观点:
<%= image_tag micropost.pictures_urls %>
我的终端回复:
Started GET
"/images/[%22/app/assets/images/37/%5B%22Thankyou.jpg%22%5D%22]" for
::1 at 2016-08-03 18:11:08 -0700 Started GET
"/images/[%22/app/assets/images/33/%5B%22Thankyou.jpg%22%5D%22]" for
::1 at 2016-08-03 18:11:08 -0700 Started GET
"/images/[%22/app/assets/images/35/%5B%22detailsaboutus.jpg%22%5D%22]"
for ::1 at 2016-08-03 18:11:08 -0700 ActionController::RoutingError
(No route matches [GET]
"/images/[%22/app/assets/images/37/%5B%22Thankyou.jpg%22%5D%22]"):
Started GET
"/images/[%22/app/assets/images/36/%5B%22GPcolors4.jpg%22%5D%22]" for
::1 at 2016-08-03 18:11:08 -0700 Started GET
"/images/[%22/app/assets/images/34/%5B%22GP-digital-logoclear.gif%22%5D%22]"
for ::1 at 2016-08-03 18:11:08 -0700 . . . .
答案 0 :(得分:1)
为了解决这个问题,我更新了模型:
class Micropost < ApplicationRecord
belongs_to :user
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 200 }
mount_uploaders :pictures, PictureUploader
serialize :pictures, Array #updated code
end
在视图中:
<% @micropost.pictures.each do |picture| %>
<article> <!--used to stylize view -->
<%= image_tag(picture.url) %>
</article>
<% end %>
当我在AWS S3和psql db中创建用于生产的Micropost模型时:图片类型为&#39; text&#39;:
rails g model Micropost pictures:text...
或
rails g migration add_field_to_microposts pictures:text
打开迁移
add_column :microposts, :pictures, :text
然后
rails db:migrate
希望这有助于另一位新秀!
答案 1 :(得分:0)
这看起来像你上传带有has_many关系的文件,所以你得到多个图像及其图像网址数组
你可以这样做
<% micropost.pictures_urls.each do |url| %>
<%= image_tag url %>
<%end%>