Tag: Ruby

Rails + Devise and remember_me (rememberable) by default

I've wanted to add an automatic remember_me for Devise by default (without a checkbox). First I've tried to do something like this:

def create
  params[:user].merge!(remember_me: true)
  super
end

Unfortunately it doesn't seem to work. I have a rememberable strategy included in my User model, but still, remember_user_token cookie was not created. I don't know why, even with remember_me option provided, it sets it to false. The easiest (and working) way to fix this, is to overwrite remember_me method, so it will always return true value.

# User of our portal
class User < ActiveRecord::Base

  devise :database_authenticatable, 
    :trackable, :encryptable, :confirmable, :recoverable,
    :registerable, :validatable, :lockable, :rememberable

  # @return [Boolean] user should be remembered when he logs in (with cookie)
  #   so he won't be asked to login again
  def remember_me
    true
  end
end

To be honest I didn't have time to investigate it more deeply, so maybe there's a better solution but this one works. After that, remember_user_token cookie is created and the session isn't lost after user closes his browser.

Update

As Christoph mentioned in one of the comments, this is the "official" way to do this: , so probably there's no better way ;)

Rails + Paperclip + Open-Uri – Downloading files from the internet and saving them with Paperclip

I don't like hotlinking. Being dependent on someone’s else infrastructure makes me a bit worried. Especially when I don't know who this "someone" is. That's why I've decided to download all the images that users embed in comments. After download, I attach them with Paperclip to their comments. Thanks to that, I can be sure, that they will always be served from my own servers (also I can use them later for some fancy stuff).

Theoretically, to obtain this, we could do something like that:

# We will use open-uri to download embedded images
require "open-uri"

file = open(image_url)
current_comment.pictures.create!(file: file)

Unfortunately this will "almost" work. If we try to get the file url:

current_picture.file.url #=> '/images/pictures/12313/file.'

we'll get it without an file extension. This happens, because open-uri creates a Tempfile without a file extension. Unluckily Paperclip uses file name to determine extension. To get around this issue, we need to create a new Tempfile with a valid extension and copy open-uri Tempfile content to newly created one:

# We will use open-uri to download embedded images
require "open-uri"

file = Tempfile.new ['', ".#{image_url.split('.').last}"]
file.binmode # note that our tempfile must be in binary mode
file.write open(image_url).read
file.rewind
file
current_comment.pictures.create!(file: file)

Update - Paperclip can do this on its own!

Posted by Aditya Sanghi (thanks a lot!):

current_comments.pictures.create!(file: URI.parse(image_url))

Although keep in mind, that you still need to handle 500, 404, etc errors (Paperclip can raise them).

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑