我是Rails的新手,我正在尝试使用carrierwave gem将文件上传到我的系统上。我已经完成了所有步骤,但一直在努力编写代码,因此我可以在系统上无任何错误地查看它。
我以前曾尝试添加
show.html.erb:
<%= attachment_tag @post.attachment.url, class: "post-show" %>
但收到错误
"undefined method `attachment_tag' for #<#<Class:0xb646078>:0xb8bb8b0>"
posts.rb:
class Post < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
end
new.html.erb:
<%= f.label :attachment %><br>
<%= f.file_field :attachment %>
show.html.erb:
<%= attachment_tag @post.attachment.url, class: "post-show" %>
/uploaders/video_uploader.rb
class VideoUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
posts_controller:
def post_params
params.require(:post).permit(:post_id, :title, :body, :date, :image, :remove_image, :video, :attachment)
end
我希望它的输出是文件的显示,无论它是pdf,ppt还是word文档。
编辑:
<iframe src="@post.attachment.url"></iframe>
此文件已添加到show.html.erb中,而不是附件标签中。我不再收到错误,但显示如下: