ruby-on-rails - Rails 金钱 gem "uninitialized constant Money::Currency::TABLE"查看

标签 ruby-on-rails rubygems currency

我对货币 gem 有疑问。我正在尝试显示所有货币的下拉菜单:

View :

<% if @receipt.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@receipt.errors.count, "error") %> prohibited this receipt from being saved:</h2>
  <ul>
    <% @receipt.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
    <% end %>
  </ul>
</div>
<% end %>
<%= simple_form_for(@receipt, :html => {:class => 'form-horizontal', :multipart => true }) do |f| %>
<h3>Kassenzettel hinzufügen</h3>
  <p>
  <div class="input-prepend">
    <%= f.label :due_date, 'Ausstellungsdatum' -%>
    <span class="add-on"><i class="icon-calendar"></i></span><%= f.text_field :due_date,  :value => Date.today.strftime('%d.%m.%Y'), :class => 'datepicker span2', :"data-date-format" => :'dd.mm.yyyy' %>
    </p>
    <p>
    <%= f.label :due_time, 'Zeit (optional)' -%>

    <%= f.time_select :due_time, { :class => "adsf"} %>
    </p>
    <p>
      <%= f.select(:currency,all_currencies(Money::Currency::TABLE), {:include_blank => 'Select a Currency'}) %>

    </p>
    <p> <%= f.label :recipt, 'Datei hochladen' -%> <%= f.file_field :document -%>
    <p class="help-block">Akzeptierte Formate: PDF, JPG, TIFF, max. 1 MB</p>
    </p>
    <br />
    <p>
      <button type="submit" class="btn btn-success"><i class="icon-plus icon-white"></i> Dokument hinzufügen </button>
      <%= link_to 'Abbrechen', receipts_path, :class => "btn" %></p>  
<% end %>

模型:

class Receipt < ActiveRecord::Base  

    belongs_to :user
    has_many   :products, :dependent => :destroy
    self.per_page = 5   
    validates :due_date, :presence => true
    validates_attachment :document, :presence => true,            
              :size => { :in => 0..5024.kilobytes }
    has_attached_file :document,
    :styles => {
        :minithumb=> ["50x50#", :png],
        :thumb=> ["150x150#", :png],
        :small  => ["500>", :png]       },
    :convert_options => { :all => '-background white -flatten +matte'}
    attr_accessible :due_date, :due_time, :receiptissuedate, :document, :dependent => :destroy
    columns_hash["due_time"] = ActiveRecord::ConnectionAdapters::Column.new("due_time", nil, "time")

    composed_of :receiptfinalamount,
        :class_name => "Money",
        :mapping => [%w(receiptfinalamount cents), %w(receiptcurrency currency_as_string)],
        :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
        :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }

    before_validation(:on => :create) do
        self.receiptissuedate = DateTime.new(self.due_date.year, self.due_date.month, self.due_date.day, self.due_time.hour, self.due_time.min)
    end

    before_save :default_values
    def default_values
        self.receiptfinalamount ||= 0
    end

    def due_date
        return @due_date if @due_date.present?
        return Date.today
    end

    def due_time
        return @due_time if @due_time.present?
        return Time.now
    end 


    def due_date=(new_date)
        @due_date = self.string_to_datetime(new_date, I18n.t('date.formats.default'))
    end


    def due_time=(new_time)
        @due_time = self.string_to_datetime(new_time, I18n.t('time.formats.time'))
    end

    protected

    def string_to_datetime(value, format)
        return value unless value.is_a?(String)

        begin
            DateTime.strptime(value, format)
        rescue ArgumentError
            nil
        end
    end

end

Controller :

class ReceiptsController < ApplicationController


  helper_method :sort_column, :sort_direction

  # GET /receipts
  # GET /receipts.json
  def index
    #@receipts = Receipt.all
    @receipts = current_user.receipts.order(sort_column + " " + sort_direction).paginate(:page => params[:page]).order('receiptissuedate DESC') 

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @receipts }     
    end
  end


  # GET /receipts/1
  # GET /receipts/1.json
  def show
    @receipt = current_user.receipts.find(params[:id])
    @products = current_user.receipts.find(params[:id]).products.all
    @product = Product.new

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @receipt }
    end
  end

  # GET /receipts/new
  # GET /receipts/new.json
  def new

    @receipt = Receipt.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @receipt }
    end
  end

  # GET /receipts/1/edit
  def edit
    @receipt = Receipt.find(params[:id])
  end

  # POST /receipts
  # POST /receipts.json
  def create
    @receipt = current_user.receipts.new(params[:receipt])

    respond_to do |format|
      if @receipt.save
        format.html { redirect_to receipts_url, notice: 'Receipt was successfully created.' }
        format.json { render json: @receipt, status: :created, location: @receipt }
      else
        format.html { render action: "new" }
        format.json { render json: @receipt.errors, status: :unprocessable_entity }
      end
    end
  end
  # PUT /receipts/1
  # PUT /receipts/1.json
  def update
    @receipt = current_user.receipts.find(params[:id])

    respond_to do |format|
      if @receipt.update_attributes(params[:receipt])
        format.html { redirect_to @receipt, notice: 'Receipt was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @receipt.errors, status: :unprocessable_entity }
      end
    end
  end


  # DELETE /receipts/1
  # DELETE /receipts/1.json
  def destroy
    @receipt = current_user.receipts.find(params[:id])
    @receipt.destroy

    respond_to do |format|
      format.html { redirect_to receipts_url }
      format.json { head :no_content }
    end
  end

  private

  def sort_column
    Receipt.column_names.include?(params[:sort]) ? params[:sort] : "receiptissuedate"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
  end
end

错误:

收据中的名称错误#new

显示/Users/dastrika/Documents/RailsProjects/receipter/app/views/receipts/_form_new.html.erb 其中第 22 行提出:

未初始化的常量 Money::Currency::TABLE

提取的源代码(第 #22 行附近):

19: <%= f.label :due_time, 'Zeit (可选)' -%> 20:
21: <%= f.time_select :due_time, { :class => "adsf"} %> 22:

23:

24: <%= f.select(:currency,all_currencies(Money::Currency::TABLE), {:include_blank => '选择货币'}) %> 25:

模板包含的痕迹:app/views/receipts/new.html.erb

Rails.root:/Users/dastrika/Documents/RailsProjects/receipter 应用追踪 |框架跟踪 |完整跟踪

app/views/receipts/_form_new.html.erb:22:in block in _app_views_receipts__form_new_html_erb__159753442_6920660' app/views/receipts/_form_new.html.erb:11:in_app_views_receipts__form_new_html_erb__159753442_6920660' app/views/receipts/new.html.erb:7:in _app_views_receipts_new_html_erb__164008852_34408570' app/controllers/receipts_controller.rb:38:in新'

最佳答案

此语法不正确

Money::Currency::TABLE

你想要

all_currencies(Money::Currency.table)

def all_currencies(hash)
  hash.keys
end

http://rubymoney.github.com/money/

关于ruby-on-rails - Rails 金钱 gem "uninitialized constant Money::Currency::TABLE"查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10812232/

相关文章:

ruby-on-rails - gem-idea : Automatic spam protection with captcha in before_filter when HTTP-method is post, 放置或删除

c++ - 计算给定零钱的方法数量

ruby-on-rails - 为什么在 rails 2 中使用 vpim 会出现编码错误?

rubygems - 构建另一个 gem 时如何使用私有(private) gem 服务器托管的 gem

ruby-on-rails - 如何从 Rails 中删除设计模型?

java - 如何通过国家代码获取货币名称?

ios - 特定价格等级的本地 App Store 货币是否可变?

ruby-on-rails - SQL Server 和 Rails 的麻烦

ruby-on-rails - ArgumentError:您需要使用:if提供至少一个验证

ruby-on-rails - SimpleCov 与 Selenium/Rails