Rails 中的模型助手并在视图中渲染

发布于 2024-12-13 04:35:27 字数 324 浏览 1 评论 0原文

我想在模型助手中编写一些方法并在我的视图中渲染。我的产品型号有价格栏。例如,在视图中我希望能够编写 @product.tier_one_quantity_one 。如何编写助手以及如何渲染?

我可以在一个方法中分配多个变量吗?

module ProductsHelper
  def price_variation(product)
    @tier_one_quantity_one = @product.price * 1.2
    @tier_one_quantity_wo = @product.price * 1.4
    ...
  end
end

I want to write some method in model helper and render in my view. My Product model has price column. In the view I want to be able to write @product.tier_one_quantity_one for example. How to write the helper and how to render?

Can I assign multiple variables in a single method?

module ProductsHelper
  def price_variation(product)
    @tier_one_quantity_one = @product.price * 1.2
    @tier_one_quantity_wo = @product.price * 1.4
    ...
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2024-12-20 04:35:27

助手采用更实用的方法:

module ProductsHelper
  def tiered_price(product, tier, quantity)
    price = case tier
      when 1 then product.price * 1.2
      when 2 then product.price * 1.4
      else product.price * 1.6
    end
    price * product.quantity
  end
end

# view
<%= number_to_currency( tiered_price(@product, 1, 2) ) %>

但在我看来,这在模型中会更好:

class Product < ActiveRecord::Base
  def tiered_price(tier, quantity)
    price = case tier
      when 1 then price * 1.2
      when 2 then price * 1.4
      else price * 1.6
    end
    price * quantity
  end
 end

 # view
 <%= number_to_currency(@product.tiered_price(1, 2)) %>

如果您确实希望能够像 find_by_name 一样编写 @product.tier_one_quanity_two ,则需要挂钩 method_missing ,这有一定的复杂性和速度开销,但会是这样的:

class Product < ActiveRecord::Base
  def method_missing(method, *args)
    match = method.to_s.match(/tier_(.*)_quantity_(.*)/)
    if match && match[0] && match[1]
      unit_price = case match[0]
        when 'one' then price * 1.2
        when 'two' then price * 1.4
        else price * 1.6
      end
      quantity = case match[1]
        when 'one' then 1
        when 'two' then 2
        #.. and so on
      end 
      unit_price * quantity
    else
      super
    end
  end
end

Helpers take a more functional approach:

module ProductsHelper
  def tiered_price(product, tier, quantity)
    price = case tier
      when 1 then product.price * 1.2
      when 2 then product.price * 1.4
      else product.price * 1.6
    end
    price * product.quantity
  end
end

# view
<%= number_to_currency( tiered_price(@product, 1, 2) ) %>

But it seems to me that this would be better in the model:

class Product < ActiveRecord::Base
  def tiered_price(tier, quantity)
    price = case tier
      when 1 then price * 1.2
      when 2 then price * 1.4
      else price * 1.6
    end
    price * quantity
  end
 end

 # view
 <%= number_to_currency(@product.tiered_price(1, 2)) %>

If you really want to be able to write @product.tier_one_quanity_two like find_by_name, you'll need to hook into method_missing, which has some complexity and speed overhead, but would go something like this:

class Product < ActiveRecord::Base
  def method_missing(method, *args)
    match = method.to_s.match(/tier_(.*)_quantity_(.*)/)
    if match && match[0] && match[1]
      unit_price = case match[0]
        when 'one' then price * 1.2
        when 'two' then price * 1.4
        else price * 1.6
      end
      quantity = case match[1]
        when 'one' then 1
        when 'two' then 2
        #.. and so on
      end 
      unit_price * quantity
    else
      super
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文