ArgumentError(Date与nil的比较失败)

时间:2016-06-24 17:07:36

标签: ruby-on-rails

我正在Rails中建立一个具有特定销售模型的商店。我需要允许用户每30天只向其订单添加3个项目。 30天计数器应该在添加第一个order_item时开始。一旦30天到期,用户将能够添加3个订单。如果30天没有通过,例如,用户添加了两个ord​​er_items,他仍然可以在30天内再添加一个order_item。因此,如果用户尝试添加超过3个项目以显示错误消息,并忽略将order_items保存到current_user的订单。 当我提交表单以将order_item添加到订单时,我收到此错误:

ArgumentError (comparison of Date with nil failed):
  app/controllers/order_items_controller.rb:4:in `<'
  app/controllers/order_items_controller.rb:4:in `create'

order_items_controller.rb:

def create
      now = Date.today
      if (now - 30) < current_user.begin_date
         if current_user.order_counter >= 3
            # deal with the case that order should not be created, 
            # for example redirect
         else
           current_user.order_counter += 1
           current_user.save
         end
      else 
        current_user.order_counter = 1
        current_user.begin_date = now
        current_user.save
      end
      @order = current_order
      @order_item = @order.order_items.new(order_item_params)
      @order.user_id = current_user.id
      @order.save
      session[:order_id] = @order.id

      respond_to do |format|
        format.js { flash[:notice] = "ORDER HAS BEEN CREATED." } 
      end
    end

形式:

 <%= form_for OrderItem.new,  html: {class: "add-to-cart"}, remote: true do |f| %>
<div class="input-group">
      <%= f.hidden_field :quantity, value: 1, min: 1 %>
      <div class="input-group-btn">
        <%= f.hidden_field :product_id, value: product.id %>
        <%= f.submit "Add to Cart", data: { confirm: 'Are you sure that you want to order this item?'}, class: "btn btn-default black-background white" %>
      </div>
    </div>
  <% end %>
</div>

2 个答案:

答案 0 :(得分:0)

我认为这应该有效:

def create
      now = Date.today
      if ((Date.today - 30)..Date.today).cover?(current_user.begin_date)
         if current_user.order_counter >= 3
            # deal with the case that order should not be created, 
            # for example redirect
         else
           current_user.order_counter += 1
           current_user.save
         end
      else 
        current_user.order_counter = 1
        current_user.begin_date = now
        current_user.save
      end
      @order = current_order
      @order_item = @order.order_items.new(order_item_params)
      @order.user_id = current_user.id
      @order.save
      session[:order_id] = @order.id

      respond_to do |format|
        format.js { flash[:notice] = "ORDER HAS BEEN CREATED." } 
      end
    end

但是,我不认为这种检查最适合您的Controller。我想你应该把支票移到你的order_items并检查

class OrderItem
  def self.total_orders_within_range(range)
    where(created_at: range).count
  end
end

class User
  has_many :order_items #, through: :orders
  def can_create_new_order_items?
    range = (Date.today - 30.days)..Date.today)
    order_items.total_orders_within_range(range) < 3
  end
end

在您的控制器的创建操作中,您现在可以拥有以下内容:

   if current_user.can_create_new_order_items?
     # you can perform a check here for how many items the user is trying to add and diff 
     # add other order creation methods here, you could also add an else to inform the user that his order couldn't be created
   end

答案 1 :(得分:0)

试试这个

  if current_user.begin_date && (now - 30) < current_user.begin_date
     if current_user.order_counter >= 3
        # deal with the case that order should not be created, 
        # for example redirect
     else
       current_user.order_counter += 1
       current_user.save
     end
  else 
    current_user.order_counter = 1
    current_user.begin_date = now
    current_user.save
  end

如果current_user.begin_date返回 Nil

,这将返回false并登陆 Else