ruby-on-rails – Rails中的DRYing视图(number_to_currency)
发布时间:2020-12-16 21:48:46 所属栏目:百科 来源:网络整理
导读:我的代码类似于: number_to_currency(line_item.price,:unit =“£”) 在各种模式中乱丢我的观点.由于我的应用程序仅以英镑(£)交易,所以我不应该将其移动到我的每个模型中,以便line_item.price返回字符串,因为它应该是(即number_to_currency(line_item.pr
我的代码类似于:
number_to_currency(line_item.price,:unit =>“£”) 在各种模式中乱丢我的观点.由于我的应用程序仅以英镑(£)交易,所以我不应该将其移动到我的每个模型中,以便line_item.price返回字符串,因为它应该是(即number_to_currency(line_item.price,:unit ="“£”))和line_item.price是一样的,我在想这样做我应该: def price number_to_currency(self.price,:unit => "£") end 但这不行.如果模型中已经定义了价格,那么Rails会报告“堆栈级别太深”,当我将def价格改为def量时,那么它抱怨number_to_currency没有定义? 解决方法
number_to_currency是一个视图助手,因此它在模型中不可用.
您可以通过在application_helper.rb中定义自己的帮助来保存一些关键笔画(因此可用于所有视图).例如 def quid(price) number_to_currency(price,:unit => "£") end 然后在视图中调用它: quid(line_item.price) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |