Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails HomeController显示未定义方法时出现问题_Ruby On Rails_Ruby_Shopify App - Fatal编程技术网

Ruby on rails HomeController显示未定义方法时出现问题

Ruby on rails HomeController显示未定义方法时出现问题,ruby-on-rails,ruby,shopify-app,Ruby On Rails,Ruby,Shopify App,我正试图将shopify存储的产品传递到Rails应用程序中,但在我进行更新时,一直出现home controller错误,运气不佳,并多次重启服务器 欢迎任何帮助。这是密码 class Api::V1::HomeController < ShopifyApp::AuthenticatedController def index @products = ShopifyAPI::Product.find(:all, params: { limit: 10 }) @products.eac

我正试图将shopify存储的产品传递到Rails应用程序中,但在我进行更新时,一直出现home controller错误,运气不佳,并多次重启服务器

欢迎任何帮助。这是密码

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
从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
class StoredProductshop-u-id{where(shop-u-id:shop-u-id)}
结束

问题是
@shop
为零。错误消息表示它无法在类上调用方法
.id

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

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

但这取决于您的代码,因此请仔细检查。

对于本特定问题/代码教程,应按如下方式设置私有设置方法:

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

另一个答案是
params
而不是
session

乐意帮忙!!
def set_shop
  @shop = Shop.find_by(id: session[:shop_id])
  set_locale
end