Rails: displaying an image from a blob field in a database

前端 未结 3 2014
后悔当初
后悔当初 2020-12-25 08:26

So, I managed to get an image blob into my MySQL database (there\'s a big hex number in the field), but I can\'t find any documentation on how to display the image in a rail

3条回答
  •  猫巷女王i
    2020-12-25 09:25

    The following code should work. In your controller, create a method:

    
    def show_image
        @user = User.find(params[:id])
        send_data @user.image, :type => 'image/png',:disposition => 'inline'
    end
    
    

    In your view:

    
    <%= image_tag url_for(:controller => "mycontroller", :action => "show_image", :id => @user.id) %>
    
    

    I would recommend using the Paperclip gem. It makes saving/viewing of images really easy.

提交回复
热议问题