I am using rails 6.1.7.7
I was using Active storage to upload a file/ image to the storage directory (config set as local). I tried to use url_for and rails_blob_url. They return me an url which dosent work. However, when I try to get the signed_id of the same blob file, replace it with the id in the url, it works!
So I believe until now, that signed_id is generated incorrect when I am asking for url. Is there any way to solve this, I have attached the code here:
<code> begin
uploaded_file = params[:image]
valid_mime_types = %w(image/jpeg image/jpg image/png)
# Validate mime type
unless valid_mime_types.include?(uploaded_file.content_type)
render json: dataResponse(nil, "Invalid image format. Only JPG, PNG, and GIF are allowed."), status: :unprocessable_entity
return
end
@blog = Blog.new(blog_params)
# ab = blog.image.attach(uploaded_file) # Attach the uploaded file using Active Storage
# @blog.image_url = @blog.image.attached? ? rails_blob_url(@blog.image, only_path: true) : nil
@blog.image_url = @blog.image.attached? ? Rails.application.routes.url_helpers.url_for(@blog.image) :nil
blob = ActiveStorage::Blob.find_by(filename: "julia-vivcharyk-zDXvAwE5Be0-unsplash.jpg")
print("Signed Id is : -> #{blob.signed_id}n")
print("Signed Id is : -> #{@blog.image_url}n")
if @blog.save
render json: dataResponse("Blog Added Successfully", nil), status: :created
else
render json: dataResponse(nil, @blog.errors), status: :bad_request
end
rescue StandardError => e
render json: dataResponse(nil, e.message), status: :unprocessable_entity
end
end
</code>
<code> begin
uploaded_file = params[:image]
valid_mime_types = %w(image/jpeg image/jpg image/png)
# Validate mime type
unless valid_mime_types.include?(uploaded_file.content_type)
render json: dataResponse(nil, "Invalid image format. Only JPG, PNG, and GIF are allowed."), status: :unprocessable_entity
return
end
@blog = Blog.new(blog_params)
# ab = blog.image.attach(uploaded_file) # Attach the uploaded file using Active Storage
# @blog.image_url = @blog.image.attached? ? rails_blob_url(@blog.image, only_path: true) : nil
@blog.image_url = @blog.image.attached? ? Rails.application.routes.url_helpers.url_for(@blog.image) :nil
blob = ActiveStorage::Blob.find_by(filename: "julia-vivcharyk-zDXvAwE5Be0-unsplash.jpg")
print("Signed Id is : -> #{blob.signed_id}n")
print("Signed Id is : -> #{@blog.image_url}n")
if @blog.save
render json: dataResponse("Blog Added Successfully", nil), status: :created
else
render json: dataResponse(nil, @blog.errors), status: :bad_request
end
rescue StandardError => e
render json: dataResponse(nil, e.message), status: :unprocessable_entity
end
end
</code>
begin
uploaded_file = params[:image]
valid_mime_types = %w(image/jpeg image/jpg image/png)
# Validate mime type
unless valid_mime_types.include?(uploaded_file.content_type)
render json: dataResponse(nil, "Invalid image format. Only JPG, PNG, and GIF are allowed."), status: :unprocessable_entity
return
end
@blog = Blog.new(blog_params)
# ab = blog.image.attach(uploaded_file) # Attach the uploaded file using Active Storage
# @blog.image_url = @blog.image.attached? ? rails_blob_url(@blog.image, only_path: true) : nil
@blog.image_url = @blog.image.attached? ? Rails.application.routes.url_helpers.url_for(@blog.image) :nil
blob = ActiveStorage::Blob.find_by(filename: "julia-vivcharyk-zDXvAwE5Be0-unsplash.jpg")
print("Signed Id is : -> #{blob.signed_id}n")
print("Signed Id is : -> #{@blog.image_url}n")
if @blog.save
render json: dataResponse("Blog Added Successfully", nil), status: :created
else
render json: dataResponse(nil, @blog.errors), status: :bad_request
end
rescue StandardError => e
render json: dataResponse(nil, e.message), status: :unprocessable_entity
end
end
I expect to have a better implementaion of the active storage in rails and any inputs are always welcome.