0

I am new to rails and I am trying to use the carrierwave gem to upload files onto my system. I have done all of the steps but struggling with how to code it so I am able to view it on my system without an error.

I have previously tried adding

    show.html.erb:

    <%= attachment_tag @post.attachment.url, class: "post-show" %>

but receive the error

   "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

I expect the output of this to be a display of the file whether it is a pdf, ppt or word document.

EDIT:

     <iframe src="@post.attachment.url"></iframe>

This was added to the show.html.erb instead of the attachment tag. I do not receive an error anymore but it displays this:

display on system once adding code

Yogi
  • 19
  • 4
  • Which line triggers that error? It's not clear from here as no line has `attachment_tag` called. – tadman Feb 08 '19 at 00:33
  • I'm not sure `attachment_tag` is in fact a method that exists. Do you know of documentation for it anywhere? Why do you think it's valid? Why not use a regular link tag? – max pleaner Feb 08 '19 at 07:39
  • If your uploading feature has finished, and the `@post.attachment.url` can return the right url of the file, you could try using Rails' `image_tag` [helper method](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag) to display the image file in template file. – Xullnn Feb 08 '19 at 09:13
  • @tadman https://stackoverflow.com/questions/5463572/upload-video-in-a-rails-application I used the reply from Sujan although it talks about a video. In the views/stream they have put <%= video_tag post.video_url.to_s :controls =>true %> – Yogi Feb 08 '19 at 12:26
  • @maxpleaner I have used https://stackoverflow.com/questions/5463572/upload-video-in-a-rails-application, reply from Sujan. How am I able to use a regular link tag? – Yogi Feb 08 '19 at 12:29
  • @Yogi if it's an image then use image_tag, if video use video_tag ... i think you're right. The point is attachment_tag doesnt exist – max pleaner Feb 08 '19 at 19:48

0 Answers0