0

我有一个模型,称为 line_items。它包含价格字段。当用户输入价格时,它可以是 foodio_price 或 delivery_price。价格字段有很多这样的值。

我想检查一下。这是我的条件:

price 字段中的所有价格应为 foodio_price 或 delivery_price。如果不是,给出错误,如果是,给出是 foodio_price 还是 delivery_price。

任何人都可以帮助实施它吗?

我做了一些很肮脏但不起作用的事情:

       @line_items.each do |i|
          if i.price == i.product.foodio_price
              @line_items.each do |i|
                if i.price == i.product.delivery_price
                  render :action => "cart"
                end
              end
          else
              @line_items.each do |i|
                if i.price == i.product.foodio_price
                  render :action => "cart"
                end
              end
          end
      end

它给出了太多渲染或重定向调用的错误

4

2 回答 2

1

线索在错误消息中——每个动作只能渲染或重定向一次。

尝试return在每次调用render.

于 2013-04-18T11:17:17.097 回答
0

我不清楚您的逻辑,但是您不能调用超过一个renderredirect每个操作,并且作为惯例,您应该只调用一个redirectrender在操作结束时调用。

但是,这可能会让您对您的逻辑有所了解:

@line_items.each do |i|
  product = i.product
  if ([product.foodio_price, product.delivery_price].include(i.price))
     #once of your prices
  else 
     #neither of your prices 
  end
end
于 2013-04-18T11:28:53.243 回答