rootstrap / active-storage-base64

Base64 support for ActiveStorage
https://rootstrap.com
MIT License
160 stars 16 forks source link

ActiveStorage::Attachment load nil #42

Closed huynhlv closed 4 years ago

huynhlv commented 4 years ago

Hi, I'm getting this error ActiveStorage::Attachment Load nil In my controller: def create pitch_owner = PitchOwner.create(user_id: 1) pitch_owner.image.attach(params[:image]) end My Parameters: {"image"=>{"data"=>"data:image/png;base64,iVBO..."}, "pitch_owner"=>{"pitch_name"=>"abc"}} Screenshot from 2019-11-11 23-22-45 In rails server, it can't load active_storage_attachments but in rails console with this code I can create success active_storage_attachments Screenshot from 2019-11-11 23-26-16 Thanks so much!

santib commented 4 years ago

Hi @huynhlv , I'm not sure I understand what's the issue. Looking at your first screenshot, line 15 looks weird, what is pitch in that line: pitch owner.image.attach(param) ?

huynhlv commented 4 years ago

Hi @huynhlv , I'm not sure I understand what's the issue. Looking at your first screenshot, line 15 looks weird, what is pitch in that line: pitch owner.image.attach(param) ?

Hi @santib, thank you for responding! VsCode not show '_' in terminal :smile: Before line this code i have set param = params[:image] My code in controller: Screenshot from 2019-11-12 00-27-11

santib commented 4 years ago

@huynhlv ok great. So just to double check, you are saying that it's working fine to you if you attach images from the rails console but it's not from the rails server (controller), right?

Would you mind providing:

  1. ruby version
  2. rails version
  3. active_storage_base64 version

?

That way we can try to reproduce the issue.

huynhlv commented 4 years ago

@santib Yes, that right!

  1. ruby version: 2.5.1
  2. rails version: 5.2.3
  3. I don't know the version but in gemfile I have add line code: gem 'active_storage_base64'
santib commented 4 years ago

@huynhlv You can check the Gemfile.lock to see which version of active_storage_base64 you are using

huynhlv commented 4 years ago

@santib Yes, It is version 0.1.4

santib commented 4 years ago

@huynhlv I think I was able to reproduce your case. If you don't "permit" the params you are using in the attach method seems that it doesn't work. Would you mind confirming it?

https://apidock.com/rails/ActionController/Parameters/permit

huynhlv commented 4 years ago

@santib I try set param1 = params.require(:image).permit(:data) When I check byebug in terminal, data param1 is: <ActionController::Parameters {"data"=>"data:image/png;base64,iVB..."} permitted: true> Then, I checked again as below Screenshot from 2019-11-12 10-01-34

santib commented 4 years ago

@huynhlv Look this is my debugging: image

try with .to_h so that you actually send a hash to the attach method instead of sending an ActionController::Parameters object.

Also make sure that you are seeing logs, maybe for some reason they are not getting displayed when you debug (for some config you might have in your project), just in case double check in log/development.log

huynhlv commented 4 years ago

@santib Thank you so much, it worked well

huynhlv commented 4 years ago

I see your README

class UsersController < ApplicationController
  def create
    user = User.create(user_params)
    user.avatar.attach(params[:avatar])
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end
santib commented 4 years ago

@huynhlv You are right, that doesn't seem to work. Would you be interested in opening a PR so that we allow ActionController::Parameters to be sent to the attach methods? The conversion to a hash could be done within the attach method.

EDIT: We should add tests that work with ActionController::Parameters in the different scenarios described by the README and make sure we either make all of them work or we update the README.