newBike
newBike

Reputation: 15002

How do I show a static html file uploaded by user

User can upload static html file on my site.

I stored the file with paperclip.

But How could I show the html in an new window with clicking the link, without downloading it.

That's say. every user can upload its resume file in HTML format

Then we can see the resume by click = User.find_by_id(ID).resume.url

But I don't want to download the html file.

I want to open an new window or hsow it in the current window or iframe

How could I do it ?

Thanks~

Upvotes: 0

Views: 84

Answers (1)

SkyWriter
SkyWriter

Reputation: 1479

It really depends on how you store them:

  1. If you store HTML in the database, you can do something like the following:

    • Add a route:

      get '/resume/:id' => 'resume#show'
      
    • Create a controller:

      class ResumeController < ApplicationController
        def show
          render html: resume_html(params[:id]).html_safe
        end
      
        private
      
        def resume_html(id)
          # here you should return your resume HTML by either
          # just returning a string
          "<h1>Resume</h1>"
          # ... or reading it from a file (this assumes UTF8 encoding)
          Paperclip.io_adapters.for(User.find_by_id(id).resume).read.
            pack('C*').force_encoding('utf-8')
        end
      end
      
  2. If you store HTML file somewhere in publicly available place like #{Rails.root}/public, then it's just a matter of issuing a redirect in the contoller (don't forget to add a route as well):

    class ResumeController < ApplicationController
      def show
        redirect_to resume_path(params[:id])
      end
    
      private
    
      def resume_path(id)
        # do whatever you need to return resume URL/path
      end
    end
    

Hope this helps!

Upvotes: 1

Related Questions