【问题标题】:Issue with HomeController showing undefined methodHomeController 显示未定义方法的问题
【发布时间】:2020-04-23 04:29:04
【问题描述】:

我正在尝试将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

【问题讨论】:

    标签: ruby-on-rails ruby shopify-app


    【解决方案1】:

    对于这个特定的问题/代码教程,私有 set_shop 方法应设置如下:

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

    另一个答案是params 而不是session

    【讨论】:

      【解决方案2】:

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

      在图像中,我可以看到参数中有一个 shop_id,因此您可能只需要在此处更改代码:

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

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

      【讨论】:

      • 乐于助人!!
      猜你喜欢
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 2011-07-22
      • 2012-03-16
      相关资源
      最近更新 更多