ruby-on-rails – 使用用户设置设置number_to_currency的单位?
发布时间:2020-12-17 01:47:42 所属栏目:百科 来源:网络整理
导读:我想允许用户在整个帐户中更改货币单位. 显而易见的方法是将unit参数传递给number_to_currency,但是给定number_to_currency在整个应用程序中使用了数百次,这似乎有点重复. 那么有什么方法可以根据每个用户在数据库中存储的设置来更改所有number_to_currency
我想允许用户在整个帐户中更改货币单位.
显而易见的方法是将unit参数传递给number_to_currency,但是给定number_to_currency在整个应用程序中使用了数百次,这似乎有点重复. 那么有什么方法可以根据每个用户在数据库中存储的设置来更改所有number_to_currency实例使用的单位? 解决方法
听起来像你需要某种全局函数/变量来定义符号
我会这样做: #app/helpers/application_helper.rb def unit User.find(current_user.id).select(:currency_type) #I don't know how your units are stored - you may need logic to return the correctly formatted unit end 这将允许您调用:<%= number_to_currency,unit:unit%> 重写助手方法 number_to_currency实际上只是一个帮助器,这意味着您可以动态附加选项: 原版的 # File actionpack/lib/action_view/helpers/number_helper.rb,line 106 def number_to_currency(number,options = {}) return unless number options = escape_unsafe_delimiters_and_separators(options.symbolize_keys) wrap_with_output_safety_handling(number,options.delete(:raise)) { ActiveSupport::NumberHelper.number_to_currency(number,options) } end 修订 #app/helpers/application_herlper.rb def number_to_currency(number,options ={}) unit = User.find(current_user.id).select(:currency_type) options[:unit] = unit unless options[:unit].present? super end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |