Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 938
Trav McKinney Avatar asked Nov 04 '22 23:11

Trav McKinney


1 Answers

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]?

like image 84
Nick Colgan Avatar answered Nov 09 '22 06:11

Nick Colgan