My question is about: How can i upload an image from URL when i'm using Active Storage. I used the code from other post here in Stackoverflow but passing through the model method, the param which i need to store in my table. The curiously situation is that i'm receiving the next error:
ActiveSupport::MessageVerifier::InvalidSignature in PostsController#update
But when i reload the show view, from this model, the images appears stored and deployed on my posts view.
Here my code in Post model:
class Post < ApplicationRecord
require 'open-uri'
has_one_attached :image_one
has_one_attached :image_two
has_one_attached :url_image
before_save :grab_image
def grab_image(url)
downloaded_image = open(url)
self.url_image.attach(io: downloaded_image, filename: "map.jpg", content_type:"image/jpg")
end
end
This is my code in the Edit Action of my Controller:
def update
@posturl = params[:post][:url_image]
@post.grab_image(@posturl)
respond_to do |format|
if @post.update!(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
I got the following links which talks about the chance to upload images from URLs but, i don´t know what else can i do:
Active Storage Overview - EdgeGuides
@omnilord/rails-5-2-activestorage-mitigating-adoption-pitfalls
