我正在使用Shopify,Ruby on Rails和Heroku postgres,我一直收到这个错误:
ActiveResource :: ForbiddenAccess(失败。响应代码= 403.响应消息=禁止。)和完成500内部服务器错误210ms(ActiveRecord:1.3ms)
我正在尝试将Shopify的数据CRUD到另一个来自rails的应用程序,我尝试迁移数据库和heroku日志以找出问题,但我没有找到。
这是我的控制器代码
def index
@products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
@webhooks = ShopifyAPI::Webhook.find(:all)
end
def show
@products = ShopifyAPI::Product.find(params[:id])
end
def new
@products = ShopifyAPI::Product.new
end
def create
@products = ShopifyAPI::Product.new(params.require(:product).permit(:title))
if @products.save
redirect_to home_index
else
render :index
end
end
这是我的路线
Rails.application.routes.draw do
resources :home
post 'home/new', to: 'home#create'
end
我的new.html.erd
<%= form_for :product do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :title %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我的index.html.erd
<% @products.each do |product| %>
<li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
<%= link_to 'Create Product', new_home_path %>
<% end %>
简单。您正在调用API并且未经过身份验证,因此您将返回403.您需要先打开经过身份验证的会话,然后再调用API。例如:
session = ShopifyAPI::Session.new(shop, token)
ShopifyAPI::Base.activate_session(session) if session.valid?