Jatin Parmar
Jatin Parmar

Reputation: 2900

Rails : how to display uploaded image

i am new to ruby on rails,i have made basic code to upload the image and save ,my code is as below

    name = params['image'].original_filename
    directory = "public/"+params[:shop]
    #check directory exits or not
     if ! File.directory?(directory)
        Dir.mkdir directory
     end
    #  create the file path
    path = File.join(directory, name)
    # write the file
    File.open(path, "wb") { |f| f.write(params['image'].read) }

and following is the code to display the uploaded image

<img src="/public/<%= @shop+"/"+@filename %>" class="img-thumbnail" alt="Cinque Terre" />

it gives the following image path on view https://cf119e25.ngrok.io/public/fxdev1.myshopify.com/chasing_dreams_by_skyway.jpg but the image is not displayed correctly.

is there anything wrong in code or need any library to use?

Upvotes: 0

Views: 984

Answers (1)

Ravi Mariya
Ravi Mariya

Reputation: 1230

you're reading the file from the public directory so

<img src="/public/<%= @shop+"/"+@filename %>" class="img-thumbnail" alt="Cinque Terre" />

should be

<img src="/<%= @shop+"/"+@filename %>" class="img-thumbnail" alt="Cinque Terre" />

so you're working URL is https://cf119e25.ngrok.io/fxdev1.myshopify.com/chasing_dreams_by_skyway.jpg

also its better if you use rails helper for image image_tag and more on asset pipeline here

Upvotes: 1

Related Questions