Basic image resizing in Ruby on Rails

前端 未结 5 1743
不思量自难忘°
不思量自难忘° 2020-12-06 01:45

I\'m creating a little photo sharing site for our home\'s intranet, and I have an upload feature, which uploads the photo at original size into the database. However, I also

5条回答
  •  温柔的废话
    2020-12-06 02:16

    There is a gem called "Refile". It is great. Go ahead and check this tutorial on how to use it. https://gorails.com/episodes/file-uploads-with-refile here is how to do it.

    Add this to your gem file

    gem 'refile', '~> 0.4.2', require: ["refile/rails", "refile/image_processing"]
    

    Create a table field using rails migration in your table as image_id of string type.Now comes how to insert into that field and display the image.

    Use this in your upload form, largely form_for do |f|

     <%= f.attachment_field :image %>
    

    if you are using rails 4 make sure you pass rails strong parameters.

    Put this in your model.rb file where you are storing the image (below class modelname)

    attachment :image
    

    displaying the image is simple.

    <%= image_tag attachment_url(current_user,:image, :fill, 200, 170, format: "jpg") %>
    

提交回复
热议问题