Closed matedemorphy closed 4 years ago
Hi @matedemorphy did you follow the README
? If you did follow it, would you mind being more specific on the lines of code you added?
Having installed ActiveStorage
and ran migrations is perfect, this gem is just an extension on top of it.
@santib this is the code:
post model
class Picking < ApplicationRecord
include ActiveStorageSupport::SupportForBase64
has_one_base64_attached :image
end
post controller
class Api::V1::PostsController < Api::V1::BaseController
def create
@post= Post.new(post_params)
if @post.save
@post.image.attach(data: params[:post][:image])
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
postman post request
{
"post": {
"title":"First Post",
"body": "content for the post",
"image": "/9j/4RNBRXhpZgAATU0AKg...."
}
}
@matedemorphy Great thanks for clarifying, I think your error is that the value of the params[:post][:image]
should be in the data uri format instead of "/9j/4RNBRXhpZgAATU0AKg...."
so you are missing a small part before the actual data.
Thanks for the answer: @santib
new postman post request
{
"post": {
"title":"First Post",
"body": "content for the post",
"image": "data:image/jpeg;base64,/9j/4RNBRXhpZgAATU0AKg...."
}
}
Now i'm getting thi error:
ActiveStorage::IntegrityError in Api::V1::PickingsController#create
Unsupported source URL: #<StringIO:0x00007fb15c7dac70>
@matedemorphy hmm just to discard a common issue, is the Content-Type
header application/json
?
if i use strong params, like this:
def create
@post= Post.create(post_params)
end
def post_params
params.require(:post).permit(:title, :body, :image)
end
Then the error is this:
ActiveSupport::MessageVerifier::InvalidSignature in Api::V1::PickingsController#create
ðŸ˜
if you want to use strong params I think you need to do it this way
def post_params
params.require(:post).permit(:title, :body, image: :data)
end
notice the data
is also whitelisted.
Did you check that your Content-Type sent in postman is fine?
@santib
In post headers: Content-Type: application/json
following your suggestion:
def post_params
params.require(:post).permit(:title, :body, image: :data)
end
I'm getting in the console: Unpermitted parameters: :image
I just realize this "error"? in console: ```Can't verify CSRF token authenticity.```
it will have something to do?, I am trying to upload the image to Cloudinary, but I am sure that this configuration is correct
oh yes seems like that could be the issue. https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html , if you are using an API I don't think you can use the Rails CSRF protection
Thanks for your help @santib
Ok, i've changed
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
for:
protect_from_forgery unless: -> { request.format.json? }
but, still
ActiveStorage::IntegrityError in Api::V1::PostsController#create
Unsupported source URL: #<StringIO:0x00007fdcdc7b05f0>
i'm using Rails 6.0.1. This is for a React Native app, i started following this tutorial, but it didn't work out for me, so i intaled active-storage-base64 gem, to add support for base64 attachments to ActiveStorage, saddly I'm going for 2 days of being with this problem and I can't make it work 😩
@matedemorphy Is it possible that those issues are related to cloudinary not to ActiveStorage itself? For example here people was getting IntegrityError because of missing credentials for AWS https://stackoverflow.com/questions/50307502/blob-error-with-active-storage-rails-5-2
Also check this https://github.com/cloudinary/cloudinary_gem/issues/366
Thnaks for keep trying to help me @santib. cloudinary is ok. meanwhile don't try to uplosd using base64, wich is exactly what the app needs.
I've allready try this:
decoded_data = Base64.decode64(data)
washed_image = MiniMagick::Image.read(decoded_data)
washed_image.format("png")
object.images.attach(
io: StringIO.new(washed_image.to_blob),
filename: "image.png",
content_type: "image/jpeg"
)
and this:
def image_io
decoded_image = Base64.decode64(params[:post][:image])
StringIO.new(decoded_image)
end
same error: Unsupported source URL: #
that was yesterday, before i found your gem.
@matedemorphy I think the error you are having is not because of this gem, but because of cloudinary + active storage. Why don't you try what they said in the comment in the issue I references before: io: washed_image.to_blob
instead of io: StringIO.new(washed_image.to_blob)
so like this
@santib please could you show the complete code that you would implement
I don't have the complete code because I don't even have the failing code, but from the issue in cloudinary I would try this
decoded_data = Base64.decode64(data)
washed_image = MiniMagick::Image.read(decoded_data)
washed_image.format("png")
object.images.attach(
io: washed_image.to_blob,
filename: "image.png",
content_type: "image/jpeg"
)
😤
undefined method `read' for #
https://github.com/cloudinary/cloudinary_gem/issues/366#issuecomment-600620959
@matedemorphy I think the error you are having is not because of this gem, but because of cloudinary + active storage. Why don't you try what they said in the comment in the issue I references before:
io: washed_image.to_blob
instead ofio: StringIO.new(washed_image.to_blob)
so like this
Yeah, seems like it cloudinary gem issue https://github.com/cloudinary/cloudinary_gem/issues/366#issuecomment-600620959
Having here almost the same problem.
farmer controller
# POST /api/v1/farmers
def create
farmer = Farmer.new(farmer_params)
if farmer.save
render json: farmer, status: :created
else
render json: farmer.errors.full_messages, status: :unprocessable_entity
end
end
def farmer_params
ActiveModelSerializers::Deserialization.jsonapi_parse(params)
end
My model:
has_one_base64_attached :picture
My request structure:
{
"data": {
"type": "farmers",
"attributes": {
"name": "None",
"address": "None",
"responsible_name": "None",
"cell_phone": "00000000000",
"picture": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QCkRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAABoBBQABAAAASgAAABsBBQABAAAAUgAAACgBAwABAAAAAgAAAGmHBAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAAABQAAkAcABA.....",
"user_attributes": {
"email": "none@none.com",
"federal_id": "111111111",
"password": "111111111"
}
}
}
}
And I got the message:
ActiveSupport::MessageVerifier::InvalidSignature (ActiveSupport::MessageVerifier::InvalidSignature):
app/controllers/api/v1/farmers_controller.rb:30:in `create'
I checked with byebug and it returned the following:
(byebug) params.require(:data).permit(:type, attributes:[:name, :address,:responsible_name,:cell_phone,picture: :data,user_attributes: %i[id email federal_id password]])
Unpermitted parameter: :picture
<ActionController::Parameters {"type"=>"farmers", "attributes"=><ActionController::Parameters {"name"=>"Pitomba Alheia", "address"=>"Quadra 44 7961 Alameda AlÃcia, São Benedito do Sul, RS 50844-983", "responsible_name"=>"Pablo Lima Jr.", "cell_phone"=>"79981040537", "user_attributes"=><ActionController::Parameters {"email"=>"sssssssssss@alheia.com.br", "federal_id"=>"11ss1sssssss2cs11ss1111112", "password"=>"123456"} permitted: true>} permitted: true>} permitted: true>
(byebug)
For some reason returns to me:
Unpermitted parameter: :picture
I don't really know why!
Got the solution:
Just changed:
"picture": {"data": "base64"}
I just tried with a simple model and a simple configuration. I've allready run active storage migrations, it could be that?