Rails 中的模型助手并在视图中渲染
我想在模型助手中编写一些方法并在我的视图中渲染。我的产品型号有价格栏。例如,在视图中我希望能够编写 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
助手采用更实用的方法:
但在我看来,这在模型中会更好:
如果您确实希望能够像 find_by_name 一样编写 @product.tier_one_quanity_two ,则需要挂钩 method_missing ,这有一定的复杂性和速度开销,但会是这样的:
Helpers take a more functional approach:
But it seems to me that this would be better in the model:
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: