Base64 encoded string to file (Ruby on Rails) - undefined `unpack 'method Error

In one of my Rails controllers, I'm trying to take Base64 encoding, decode it and write to a file (.png). Here is my code:

def create_character
    @character = Character.new(params[:character])
    @base64 = params[:base64]
    File.open("app/assets/images/characters/#{@character.name.gsub(/\s+/, "")}-#{@character.author_name.gsub(/\s+/, "")}.png", 'wb') do |f|
        f.write(Base64.decode64(@base64))
    end

    if @character.save
        flash[:notice] = "Character created."
        redirect_to(:action => 'share')
    else

I get the following error:

undefined method `unpack' for #<ActiveSupport::HashWithIndifferentAccess:0x1044b22d8>

What's going on here?

Edit: One REALLY weird that the code to write the file works fine in the rails console, but not when the application starts.

+5
source share
2 answers

It looks like you are trying to pass a hash to a decoding method. Are you sure you shouldn't @base64 = params[:character][:base64]?

+2
source

I had an error unpackwhen I changed the data type from string to text.

0
source

All Articles