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.
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]
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With