我正在尝试将stored_products从shopify传递到Rails应用程序中,但是在https://f588240c.ngrok.io/上不断得到一个主控制器错误--我已经做了一些更新,没有运气,并多次在没有运气的情况下重新启动服务器。
任何帮助都将受到欢迎。这是代码
class Api::V1::HomeController < ShopifyApp::AuthenticatedController
def index
@products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
@products.each do |product|
StoredProduct.where(shopify_id: product.id)
.first_or_create do |stored_product|
stored_product.shopify_id = product.id
stored_product.shopify_title = product.title
stored_product.shopify_handle = product.handle
stored_product.shopify_image_url = product.image.src
stored_product.shop_id = @shop.id
stored_product.save
product.images.each do |image|
ProductImage.where(shopify_id: image.id)
.first_or_create do |product_image|
product_image.image_url = image.src
product_image.stored_product_id = stored_product_id
product_image.shopify_id = image.id
end
end
end
end
@stored_products = StoredProduct.belongs_to_shop(@shop.id)
end
end
从认证的控制器
private
def set_shop
@shop = Shop.find_by(id: session[:shopify])
set_locale
end
来自store_products.rb文件
class StoredProduct < ApplicationRecord
belongs_to :shop
has_many :product_images
scope :belongs_to_shop, -> shop_id { where(shop_id: shop_id) }
end
发布于 2020-05-29 10:48:49
对于这个特定的问题/代码教程,私有的set_shop方法应该设置如下:
def set_shop
@shop = Shop.find_by(id: session[:shop_id])
set_locale
end
另一个答案是params
而不是session
发布于 2020-04-22 21:25:12
问题是@shop
是零的。错误消息说它不能在.id
上调用NilClass方法。
在图像中,我可以看到在params中有一个shop_id,所以您可能只需要在这里修改代码:
def set_shop
@shop = Shop.find_by(id: params[:shop_id])
set_locale
end
但这取决于你的代码,所以请再检查一遍。
https://stackoverflow.com/questions/61379391
复制相似问题