0

我的模型中有一个属性价格,目前它正在以科学计数法格式化,我宁愿以十进制表示法显示。

ruby-grape建议使用以下代码自定义格式化字段:

class ExampleEntity < Grape::Entity
  expose :formatted_value
  # ...
private

  def formatted_value
    "+ X #{object.value}"
  end
end

但是,我注意到value是对象的属性,在此范围内不可用。这里是#{object.methods} 的输出:

[
{
"price": "0.12E3",
"formatted_value": "+ X [:rehash, :to_hash, :to_h, :to_a, :inspect, :to_s,    :==, :[], :hash, :eql?, :fetch, :[]=, :store, :default, :default=,     :default_proc, :default_proc=, :key, :index, :size, :length, :empty?,     :each_value, :each_key, :each_pair, :each, :keys, :values, :values_at,  :shift, :delete, :delete_if, :keep_if, :select, :select!, :reject, :reject!,  :clear, :invert, :update, :replace, :merge!, :merge, :assoc, :rassoc,  :flatten, :include?, :member?, :has_key?, :has_value?, :key?, :value?,  :compare_by_identity, :compare_by_identity?, :any?, :transform_keys,  :transform_keys!, :stringify_keys, :stringify_keys!, :symbolize_keys,  :to_options, :symbolize_keys!, :to_options!, :assert_valid_keys,  :deep_transform_keys, :deep_transform_keys!, :deep_stringify_keys,  :deep_stringify_keys!, :deep_symbolize_keys, :deep_symbolize_keys!,  :reverse_merge, :reverse_merge!, :reverse_update, :with_indifferent_access,  :nested_under_indifferent_access, :blank?, :extractable_options?,  :deep_merge, :deep_merge!, :except, :except!, :slice, :slice!, :extract!,  :to_query, :to_param, :to_xml, :to_json, :entries, :sort, :sort_by, :grep,  :count, :find, :detect, :find_index, :find_all, :collect, :map, :flat_map,  :collect_concat, :inject, :reduce, :partition, :group_by, :first, :all?,  :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by,  :each_with_index, :reverse_each, :each_entry, :each_slice, :each_cons,  :each_with_object, :zip, :take, :take_while, :drop, :drop_while, :cycle,  :chunk, :slice_before, :slice_after, :slice_when, :lazy, :to_set, :present?,  :presence, :acts_like?, :try, :try!, :psych_to_yaml, :to_yaml,  :to_yaml_properties, :nil?, :===, :=~, :!~, :<=>, :class, :singleton_class,  :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?,  :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods,  :private_methods, :public_methods, :instance_variables,  :instance_variable_get, :instance_variable_set, :instance_variable_defined?,  :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send,  :public_send, :respond_to?, :extend, :display, :method, :public_method,  :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for,  :class_eval, :silence_warnings, :enable_warnings, :with_warnings,  :silence_stderr, :silence_stream, :suppress, :capture, :silence, :quietly,  :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]"   
}
]

所以下面的代码抛出了我 {“error”:“undefined method `price' for Hash:0x0055a87576f660”}

def formatted_price
 "#{object.price}"
end
4

1 回答 1

0

正如例外所说,暴露的对象是一个散列。因此,可以使用 object[:value] 而不是 object.value 找到它的属性,如下所示

        private
          def formatted_price
            "%f" % "#{object[:price]}"
          end
        end
于 2016-06-07T12:16:20.563 回答