Trav McKinney
Trav McKinney

Reputation: 998

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

In one of my Rails controllers I am trying to take a Base64 encoded string, decode it and write it 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 am getting the following error:

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

What is going wrong here?

Edit: One REALLY strange thing is that the code to write the file works perfectly fine in rails console but not when running the application.

Upvotes: 5

Views: 5504

Answers (2)

Kirk Powell
Kirk Powell

Reputation: 910

I had an unpack error when I modified the data type from string to text.

Upvotes: 0

Nick Colgan
Nick Colgan

Reputation: 5508

It looks like you're trying to pass a hash into the decode method. Are you sure you shouldn't be doing @base64 = params[:character][:base64]?

Upvotes: 2

Related Questions