目标:无需链接到新页面即可使用模式更新现有记录。
问题:我想我的问题是我无法在表单的页面上找到确切的记录,因为在提交表单之前我不知道这一点。
ShopProduct控制器:
def new
@shop_product = ShopProduct.new
end
def create
@shop_product = ShopProduct.new(shop_product_params)
@shop = Shop.find_by(params[:shop_id])
product = Product.find_by(params[:product_id])
@shop_product.product_id = product.id
@shop_product.shop_id = @shop.id
if @shop_product.save!
redirect_to '/'
flash[:notice] = "saved"
else
redirect_to '/'
flash[:notice] = "no saved"
end
end
def update
@shop_product = ShopProduct.find_by(store_variant_id: params[:store_variant_id])
respond_to do |format|
if @shop_product.update_attributes!(product_id: params[:product_id], sync: params[:sync])
format.html { redirect_to @shop_product, notice: 'Shop product was successfully updated.' }
format.json { render :show, status: :ok, location: @shop_product }
else
format.html { render :edit }
format.json { render json: @shop_product.errors, status: :unprocessable_entity }
end
end
end
除了链接到新页面外,我只能想到直接在
上进行定义我从此ShopDashboardController加载表单:
def product_variants
@shop = Shop.find(params[:shop_id])
session = ShopifyAPI::Session.new(domain: @shop.shopify_domain, token: @shop.shopify_token, api_version: '2019-04')
ShopifyAPI::Base.activate_session(session)
@in_store_products = ShopifyAPI::Product.find(:all)
@in_store_product = ShopifyAPI::Product.find(params[:shopify_product_id])
@in_store_variants = ShopifyAPI::Variant.find(:all, params: { product_id: params[:shopify_product_id]})
@shop_products = ShopProduct.where(shop_id: @shop)
@products = Product.all
@shop_product = ShopProduct.find_or_create_by(store_variant_id: params[:store_variant_id])
end
现在,如上所述,任何ShopProduct的唯一唯一记录是id和store_variant_id ...如果我在find_by
中使用def product_variants
,则由于未加载页面而无法加载能够识别@shop_product。我无法传递这些参数,因为可能有多个store_variant_id,所以我只传递了Shop.id和ShopProduct.store_product_id。但是store_product_id并不是唯一的标识符,因为多个记录可以具有相同的标识符。唯一的唯一记录是id和store_variant_id。
表单(variant
来自do循环):
<% @in_store_variants.each do |variant| %>
...
<%= form_for @shop_product do |f| %>
<%= f.collection_select :product_id, @products, :id, :sku %>
<%= f.hidden_field :store_product_id, value: variant.product_id %>
<%= f.hidden_field :store_variant_id, value: variant.id %>
<%= f.hidden_field :shop_id, value: @shop.id %>
<%= f.hidden_field :sync, value: true %>
<%= f.submit "Sync" %>
...
<% end %>
我只能创建新记录。
当我再次使用该表单进行更新时,我得到:
ActiveRecord::RecordInvalid (Validation failed: Store variant has already been taken):
app/controllers/shop_products_controller.rb:61:in `create'
模型ShopProduct:
belongs_to :product
has_one :shop
has_one :order
validates :store_variant_id, uniqueness: true, on: :create
如果记录存在,是否不应该更新?还是在这里我想念的东西?
有可能单独使用rails / ruby追求我的目标,还是需要javascript?
更新:
我试图像这样在前端定义ShopProduct:
<% @in_store_variants.each do |variant| %>
<% shop_product = @shop_products.find_by(store_variant_id: variant.id) %>
<%= form_for shop_product do |f| %>
<%= f.collection_select :product_id, @products, :id, :sku %>
<%= f.hidden_field :store_product_id, value: variant.product_id %>
<%= f.hidden_field :store_variant_id, value: variant.id %>
<%= f.hidden_field :shop_id, value: @shop.id %>
<%= f.hidden_field :sync, value: true %>
<%= f.submit "Sync" %>
<% end %>
提交时:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gaMboYCSE8v63TVzmgx4pZDMhoz205f1MV+VMhmFA/WWhVh5Pcu6u/qayU8lDmjeRXw==", "shop_product"=>{"product_id"=>"1", "store_product_id"=>"1965345", "store_variant_id"=>"19364273", "shop_id"=>"1", "sync"=>"true"}, "commit"=>"Sync", "id"=>"12"}
错误:
NoMethodError (undefined method `update' for nil:NilClass):
或具有更新属性:
NoMethodError (undefined method `update_attributes!' for nil:NilClass):
如果找到它,那不行吗?参数已通过
答案 0 :(得分:0)
这是因为该表单仅满足create
操作。通常,如果您需要更新resource
,请转到/shop_products/:id/edit
。
但是,如果您真的想重用该表单,则添加更多条件会有点复杂,但是您想要的是向PUT
发送一个/shop_products/:id
请求,它将调用{{1 }}控制器的动作。默认情况下,表单发送#update
请求,因此请考虑这一点。