HomeController显示未定义方法的问题

问题描述 投票:0回答:1

我试图将 shopify 的 stored_products 传递到 Rails 应用中,但一直得到一个主控制器的错误信息。https:/f588240c.ngrok.io. 我已经做了更新,没有运气,重启服务器多次,没有运气。

enter image description here

任何帮助将受到欢迎。以下是代码

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
ruby-on-rails ruby shopify-app
1个回答
0
投票

对于这个具体的issueecode教程,private set_shop方法应该像下面这样设置。

def set_shop
  @shop = Shop.find_by(id: session[:shop_id])
  set_locale
end

另一个答案是 params 而不是 session


0
投票

问题是 @shop 是nil。错误信息说它不能调用 .id 在NilClass上。

在图片中,我可以看到你的params中有一个shop_id,所以你可能只需要修改你的代码。

def set_shop
        @shop = Shop.find_by(id: params[:shop_id])
        set_locale
      end

但这取决于你的代码,所以请仔细检查。

© www.soinside.com 2019 - 2024. All rights reserved.