Ruby on rails 检查是否存在对象故障-Rails 5

Ruby on rails 检查是否存在对象故障-Rails 5,ruby-on-rails,Ruby On Rails,我正在尝试设置一个“添加到购物车”按钮,在用户将产品添加到购物车后,该按钮将被禁用,除非产品从购物车中删除 我正在尝试.present?,但无论产品是否已在购物车中,它似乎都忽略了这一点。即使我的购物车是完全空的,它仍然显示禁用按钮 有什么线索可以帮我解决吗 查看(产品展示): 产品控制员: class ProductController < InheritedResources::Base before_action :set_product, only: [:show]

我正在尝试设置一个“添加到购物车”按钮,在用户将产品添加到购物车后,该按钮将被禁用,除非产品从购物车中删除

我正在尝试
.present?
,但无论产品是否已在购物车中,它似乎都忽略了这一点。即使我的购物车是完全空的,它仍然显示禁用按钮

有什么线索可以帮我解决吗

查看(产品展示):


产品控制员:

class ProductController < InheritedResources::Base
  before_action :set_product, only: [:show]

    def show
        @line_item = current_order.line_items.new
    end

    def set_product
      @product = Product.find_by(product_number: params[:product_number])
    end

end
class ProductController
模型

class Order < ApplicationRecord
  has_many :line_items
  belongs_to :user, optional: true


end
类顺序
行项目模型

class LineItem < ApplicationRecord
  belongs_to :order, optional: true
  belongs_to :product, optional: true
  belongs_to :service, optional: true
end
类行项
服务模式

class Service < ApplicationRecord
    has_many :line_items
end
类服务
产品模型

class Product < ApplicationRecord
    has_many :line_items
end
类产品
问题在于您正在从产品端查看LineItem。这意味着,如果产品有任何行项目,它将禁用该按钮。因此,如果用户A已经订购了一个产品,那么该按钮将为每个人隐藏

您需要更改条件:

 <% if @product.price.present? %>
   <% if @line_item.where(product: @product).empty? %>
       <%= form_for @line_item do |f| %>
          <%= f.hidden_field :product_id, value: @product.id %>                                                       
          <%= f.submit "Add to cart" %>
       <% end %>
  <% else %>
      <%= button_to "Added to cart", "", class: "", disabled: true %>           
  <% end %>                               
<% end %>


总的来说,我确实觉得这对于视图来说有点过于合理,但这可能是一个不同的讨论。

问题在于您是从产品方面查看LineItem的。这意味着,如果产品有任何行项目,它将禁用该按钮。因此,如果用户A已经订购了一个产品,那么该按钮将为每个人隐藏

您需要更改条件:

 <% if @product.price.present? %>
   <% if @line_item.where(product: @product).empty? %>
       <%= form_for @line_item do |f| %>
          <%= f.hidden_field :product_id, value: @product.id %>                                                       
          <%= f.submit "Add to cart" %>
       <% end %>
  <% else %>
      <%= button_to "Added to cart", "", class: "", disabled: true %>           
  <% end %>                               
<% end %>


总的来说,我确实觉得这对于视图来说有点过于合理,但这可能是一个不同的讨论。

尝试类似的方法来检查产品是否已经在购物车中

def show
  @line_item = current_order.line_items.new
  @product_already_in_the_cart = current_order.line_items.pluck(:product_id).include? @product.id
end
然后对视图中的if语句使用购物车中的
@product\u ready\u

unless @product_already_in_the_cart

尝试类似的方法来检查产品是否已经在购物车中

def show
  @line_item = current_order.line_items.new
  @product_already_in_the_cart = current_order.line_items.pluck(:product_id).include? @product.id
end
然后对视图中的if语句使用购物车中的
@product\u ready\u

unless @product_already_in_the_cart

在用户将产品添加到购物车后,是否会重新加载产品展示页面的这一部分?如果没有,您将得到
@product.line\u items.blank?
=>
true
,因为它最初是空的。为什么不检查
当前订单。line\u items
是否包括产品?
当前订单。line\u items。新的
也应该是
当前订单。line\u items.build
()在用户将产品添加到购物车后,是否会重新加载产品展示页面的这一部分?如果没有,您将得到
@product.line\u items.blank?
=>
true
,因为它最初是空的。为什么不检查
当前订单。line\u items
是否包括产品?
当前订单。line\u items。新的
也应该是
当前订单。line\u items.build
()谢谢。标记为正确,因为这是更优雅的方法。谢谢。标记为正确,因为这是更优雅的方法。