not able to add images to seeds.rb ruby on rails - ruby

I am trying to add images to my seeds.rb in my ruby on rails app. However the images do not save and I'm not able to show them in my app. When I run rails db:seed, I do not get an error. I am using active storage.
I hope you can help me out!
product-card.html.erb
seeds.rb

You want to persist the record before you do the attaching. Here's an example from my seed file:
person =
FactoryBot.create(
:person,
account:,
name: Faker::TvShows::FamilyGuy.unique.character
)
person.avatar.attach(
filename: 'avatar.jpg',
io: URI.open('https://api.lorem.space/image/face')
)

Related

How to do a basic GraphQL query to Shopify with Ruby

I'm trying to do a basic GraphQL query to a Shopify store with Sinatra. Could someone help me figure out what I'm doing wrong? I looked at their API to do this:
require 'shopify_api'
require 'sinatra'
class App < Sinatra::Base
get '/' do
shop = 'xxxx.myshopify.com'
token = 'shpat_xxxxxxxxxxxxxxxxxxxxxx'
session = ShopifyAPI::Session.new(domain: shop, token: token, api_version: "2021-04")
ShopifyAPI::Base.activate_session(session)
ShopifyAPI::GraphQL.initialize_clients
client = ShopifyAPI::GraphQL.client
SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'
{
shop {
name
}
}
GRAPHQL
result = client.query(SHOP_NAME_QUERY)
result.data.shop.name
end
end
Which gives this error but I don't want to use Rake or Rails. Is it possible to do a GraphQL query to Shopify with Ruby?
ShopifyAPI::GraphQL::InvalidClient at /
Client for API version 2021-04 does not exist because no schema file exists at `shopify_graphql_schemas/2021-04.json`. To dump the schema file, use the `rake shopify_api:graphql:dump` task
As the error states, you need to first dump the schema (see this link: https://github.com/Shopify/shopify_api/blob/v9/docs/graphql.md#dump-the-schema).
Then you create a shopify_graphql_schemas directory in the same root as your ruby script, and put the generated JSON there.
Like stated in the comments, this requires a Rake task, so you need to be using Rails.
If your project doesn't use Rails, you need to do a quick workaround.
You create a temporary barebones Rails project, then generate the dump using that project (you can delete the project when you're done with this).
It's a bit hacky, but it's the only thing I can see that would work.
New link to schema dump
https://github.com/Shopify/shopify_api/blob/v9/docs/graphql.md#dump-the-schema
You need to use something like this
rake shopify_api:graphql:dump SHOP_DOMAIN="SHOP_NAME.myshopify.com" ACCESS_TOKEN="SHOP_TOKEN" API_VERSION=2022-04
Old one doesn't work anymore

Rails 5 app. Asset pipelining and precompilation with heroku not showing up images from js.jsx files

I am almost done with upgrading my Rails3 app to Rails5; But I am facing a problem with assets pipelining and precompiling. We're using cdn as asset host.Now, What happens is that when I set config.assets.precompile to false in staging environment, the app doesn't load images from js.jsx files. At other places, the app is fetching the static assets (js,css,images) from the asset_host link that I've provided. But in some specific javascript files, The images is being pointed out to my app's domain like app.my_domain.com/assets/my_image.png,And it is giving 404 not found error.
In javascript, the code is something like
return (
<img src="/assets/my_image.png"></img>
)
Since this is a js file, I cannot use asset_path helper method here.
How to resolve this issue ?
PS: setting config.assets.precompile to true loads the image from app.my_domain.com/assets/my_image.png, but that's not what I want because of this. config.assets.compile=true in Rails production, why not?
Any help with this is highly appreciated. Thanks in advance.
I found an answer after some research. I changed the name of the file to js.jsx.erb and accordingly used asset_path helper method which rails provide.
return (
<img src = "asset_path 'my_image.png'"/>
)
It works.

Jekyll site via Sinatra and Heroku - can't route to new posts

I created a 'Hello, World' app using Sinatra and then pushed to Heroku and all worked.
I've since created a basic Jekyll blog, and am trying to access it via Heroku using the following routes:
get '/?' do
file.read("_site/index.html")
end
get '/.*.*' do
file.read("_site/#{params[:splat]}")
end
not_found do
file.read("_site/error/index.html")
end
The route to the index works fine link to my site
but as soon as I click to the first post it always fails.
I have tried so many variations of different routes for the :splat and get, just can't seem to get it to work? Any ideas?
In the route that's failing, before the file.read statement, add warn "splat = #{params[:splat]}" and that will output the result to the terminal, and you can see what it's actually getting, e.g.
get '/.*.*' do
warn "splat = #{params[:splat]}"
file.read("_site/#{params[:splat]}")
end
You could also try using an absolute path to the files, though if you're getting the index page then it suggests it's not needed:
config do
set :statics, File.expand_path(File.join(settings.root, "_site"))
end
get '/.*.*' do
file.read( File.join settings.statics, params[:splat] )
end
Unless there's something else you were planning to use Sinatra's routes for, you could probably remove the Sinatra routes entirely and just make the "_site" folder the public_folder, and then Sinatra will do the serving of the static files for you:
config do
set :public_folder, File.expand_path(File.join(settings.root, "_site"))
end
# no more to do...

Issue while using Cloudinary, Carrierwave, Ckeditor with Rails Admin

I am trying to use Ckeditor with Rails Admin, where I am using Carrierwave and cloud storage as Cloudinary. After making all the settings I can see, CKeditor is able to save the file on local storage and then it creates a Cloudinary URL where the image should actually be stored. But the problem is that the image is not uploaded from that local folder to Cloudinary, whereas my simple file upload works correctly, without any issue.
One more question which I have here is - what should be the storage name when I am using Cloudinary? As for file and Amazon S3, we have names as file and s3.
Please respond.
Thanks
Cloudinary's Ruby GEM includes a plugin for CarrierWave that is used by many of our customers. We are not aware of special issues with Ckeditor (but we haven't tested it though).
When you use Cloudinary's plugin for CarrierWave simply add include Cloudinary::CarrierWave to your uploader class. It defines Cloudinary both as the storage engine and image manipulation service (both are cloud-based). Simply comment out the storage :file line in your uploader class. All images will be uploaded directly to Cloudinary and all transformed versions will be generated using Cloudinary URLs.
Please take a look at the sample uploader code in the documentation page:
http://cloudinary.com/documentation/rails_integration#carrierwave_upload
If the problem persists, it would help if you can share your uploader code so we can help making sure it is defined correctly.
I also have issue with ckeditor. Edit your CkeditorAttachmentFileUploader to look similar to this:
class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
include Cloudinary::CarrierWave
[:extract_content_type, :extract_size, :extract_dimensions].each do |method|
define_method :"#{method}_with_cloudinary" do
send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
{}
end
alias_method :"#{method}_without_cloudinary", method
alias_method method, :"#{method}_with_cloudinary"
end
def extension_white_list
Ckeditor.attachment_file_types
end
end
After that, you will find another error.
I found that in Ckeditor::AssetResponse#asset_url
method, the asset object is not reloaded, so asset.content_url will always be nil thus caused the error. I fixed it like this:
class Ckeditor::Picture < Ckeditor::Asset
...
def url_content
url(:content) || begin
if persisted?
reload
url(:content)
end
end
end
end
And similarly for Ckeditor::AttachmentFile class if you have it.
Make sure that you remove any references to CarrierWave.config.storage = :file
There are a few places you need to do this if this if uploading is not working:
In /config/initializers/carrierwave_init.rb remove all references to:
config.storage = :file
In your uploaders remove and reference of the sort:
storage :file

Two Sinatra Apps, shared datamapper DB

I'm trying to think of a good way of using Sinatra and Datamapper to create a solid web app that shares a database. I started using just one app, but I'm thinking about splitting the admin aspect to it's own app with a different mapping in a config.ru file.
So, config.ru will map the root route to the main app, and '/admin' to the Admin app. The main app will be responsible for showing data from the db to any user, and the Admin app is responsible for allowing admins to add content to the db. My reasoning behind this is to allow me more flexibility and reusability with the admin app.
The main app has the Datamapper classes responsible for defining my model, though I am sure I could move that to the admin app without a problem. Is there a way/reason to move it to it's own separate .rb file?
Obviously new to this platform, but I'm loving it. Any help is greatly appreciated.
You can have one project with as many sinatra apps as you like. for your project, you can set it up like this:
# site.rb
require 'config/boot'
class Site < Sinatra::Base
...
end
# admin.rb
require 'config/boot'
class Admin < Sinatra::Base
...
end
# config/boot.rb
require 'model/user.rb'
require 'model/post.rb'
# config.ru
require 'admin'
require 'site'
run Rack::URLMap.new("/" => Site.new, "/admin" => Admin.new)
The key is to put both apps in the same project / version control.

Resources