环境:Rails 3.2 项目
在表单视图中,我有:
<%= f.text_field :number_of_units, :value => 2 %>
<%= f.text_field :price_in_cents, :value => 20 %>
呈现的页面正确显示了我的价值 = 2 的单位数,但我的美分价格显示“NaN”或空白
我已经尝试了很多事情,但看起来我无法以这种形式为 :price_in_cent 或 :currency 设置值。其他领域工作正常。
这里是完整的表格:
<%= form_for(@pricing, :html => {:class => "form-inline"}) do |f| %>
<fieldset>
<legend><strong><%= @pricing.service.title %></strong> <%= I18n.t('pricing.title') %></legend>
<%= render :partial => 'layouts/error_explanation', :locals => {:object => @pricing} %>
<%= debug @pricing %>
<%= f.text_field :service_id %><br/>
<%= f.text_field :currency, :value => Money::Currency.table[:nok][:iso_code] %>
<%= f.text_field :pricing_unit_id %>
<%= f.text_field :number_of_units, :value => 2 %>
<%= f.text_field :price_in_cents, :value => 200 %>
<%= f.submit I18n.t('pricing.buttons.accept'), class: 'btn btn-success pull-right' %>
</fieldset>
<% end %>
这也发生在我的货币领域。所有其他字段均已正确分配。
如果我点击保存按钮,我显然会得到货币和 price_in_cents 的空值。
我尝试为模型中的所有字段添加 attr_accessible 并没有帮助。
为什么会发生这种情况?
GENERATED HTML 似乎是正确的,并且值是正确的,只有 price_in_cents 和货币在屏幕上是空白的(?):
<form accept-charset="UTF-8" action="/pricings" class="form-inline" id="new_pricing" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="ljVdzc4UNYi/RKIUqmKNd7e/PNc89BcM8F7ZhX9ViJc="></div>
<fieldset>
<legend><strong>Fish</strong> Prisnivå</legend>
<pre class="debug_dump">--- !ruby/object:Pricing
attributes:
id: !!null
creative_service_id: 46
price_in_cents: 150000
currency: NOK
pricing_unit_id: 4
number_of_units: 1
created_at: !!null
updated_at: !!null
</pre>
<input id="pricing_creative_service_id" name="pricing[creative_service_id]" size="30" type="text" value="46"><br>
<input id="pricing_currency" name="pricing[currency]" size="30" type="text" value="NOK">
<input id="pricing_pricing_unit_id" name="pricing[pricing_unit_id]" size="30" type="text" value="4">
<input id="pricing_number_of_units" name="pricing[number_of_units]" size="30" type="text" value="1">
<input id="pricing_price_in_cents" name="pricing[price_in_cents]" size="30" type="text" value="150000">
<input id="pricing_price" name="pricing[price]" type="hidden" value="NaN">
<span style="font-size:24px;text-align:center;">Nybegynner: NOK 1500 pr.dag</span>
<input class="btn btn-success pull-right" name="commit" type="submit" value="Jeg aksepterer prisbetingelsene">
</fieldset>
</form>