【问题标题】:Upload image from computer to Shopify assets [rails]将图像从计算机上传到 Shopify 资产 [rails]
【发布时间】:2018-08-11 00:37:31
【问题描述】:

我正在开发一个带有 rails 的 Shopify 应用,它可以修改商家商店的当前主题。

我正在尝试创建一个表单从应用程序上传图像(来自您计算机的本地文件)并将其直接保存在主题/资产中

我试过这个:

控制者:

 def uploadImage
  if request.post?
    p = ShopifyAPI::Asset.new
    p.key = "assets/image.png"
    p.attach(params[:image])
    p.save
  end
 end

查看:

<form method="POST" action="uploadImage">
    <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">     
       <label>Upload Image</label>
       <input type="file" name="image">            
    <button type="submit" name="button">Upload</button>
    </div>
</form>

但它不起作用,有什么想法吗?

谢谢。

【问题讨论】:

    标签: html ruby-on-rails forms image shopify-app


    【解决方案1】:

    On the documentation page,没有POST端点,意思是资产不能自己创建,必须归主题所有

    该文档可能不完整。可以比较变体,因为它们必须由产品拥有;对于这个特定的部分,他们确实记录了POST /admin/products/#{id}/variants.json(他们应该记录了POST /admin/themes/#{id}/assets.json的资产)

    You have your solution in the code source,您需要指定主题 id,所以应该可以:

    p = ShopifyAPI::Asset.new(key: "assets/image.png", theme_id: _your_theme_id)
    p.attach(params[:image])
    p.save
    

    没试过,所以你可能需要先在本地保存文件,然后将代码中的附加行替换为以下内容:

    p.attach(File.read('local/path/to/your/file.ext'))
    

    see here

    【讨论】:

    • @Gyntonic 有帮助吗?
    猜你喜欢
    • 2016-04-03
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    相关资源
    最近更新 更多