Ruby on rails 参数未保存在Rails 3应用程序中
我是Rails 3的初学者,正在开发一个允许用户输入货币值的应用程序。我正在使用jQuery插件()在编辑页面上将十进制值显示为货币值。编辑时,所有十进制属性都作为货币进行操作。保存时,正确保存采购价格和资本公积。调用属性before_save函数,并将货币值($123.45)转换为十进制值(123.45) 问题是,如果我只是编辑租金价格,关联的租金值永远不会保存。我可以看到参数中发送了正确的值,但租金模型中的before_save代码从未触发。如果编辑公寓编号值和租金价格,则租金价格将正确保存。而且,在那之后,我可以编辑我之前修改过的公寓号码的价格。但是,任何其他租金价格将不会更新 我正在使用MySQL和Rails 3.0.9 复制步骤: 非发行Ruby on rails 参数未保存在Rails 3应用程序中,ruby-on-rails,Ruby On Rails,我是Rails 3的初学者,正在开发一个允许用户输入货币值的应用程序。我正在使用jQuery插件()在编辑页面上将十进制值显示为货币值。编辑时,所有十进制属性都作为货币进行操作。保存时,正确保存采购价格和资本公积。调用属性before_save函数,并将货币值($123.45)转换为十进制值(123.45) 问题是,如果我只是编辑租金价格,关联的租金值永远不会保存。我可以看到参数中发送了正确的值,但租金模型中的before_save代码从未触发。如果编辑公寓编号值和租金价格,则租金价格将正确保存
class Property < ActiveRecord::Base
before_save :handle_before_save
has_many :rents, :dependent => :destroy
accepts_nested_attributes_for :rents, :allow_destroy => true
def handle_before_save
if new_record?
generate_default_rent_data
end
remove_currency_formatting
end
def generate_default_rent_data
10.times do |i|
self.rents.build(:apartment_number => i+1)
end
end
def remove_currency_formatting
if self.capital_reserves.to_s != self.capital_reserves_before_type_cast.to_s
self.capital_reserves = Property.remove_currency_format(self.capital_reserves_before_type_cast)
end
if self.purchase_price.to_s != self.purchase_price_before_type_cast.to_s
self.purchase_price = Property.remove_currency_format(self.purchase_price_before_type_cast)
end
end
#
# handles removing all characters from currency objects
# except for 0-9 and .
#
def self.remove_currency_format(currency_attribute)
currency_attribute.gsub(/[^0-9.]/, "")
end
end
class属性:破坏
接受\u嵌套的\u属性\u for:rents,:allow\u destroy=>true
保存前的def句柄
如果有新的记录?
生成默认租金数据
结束
删除\u货币\u格式
结束
def生成默认租金数据
10.3倍于我|
self.rents.build(:公寓号=>i+1)
结束
结束
def删除\u货币\u格式
如果self.capital_.to_!=自有资本在类型转换前的准备金
self.capital\u reserves=Property.remove\u currency\u格式(self.capital\u reserves\u type\u cast之前)
结束
如果自购价格为自购\u价格\u类型\u铸造前\u
self.purchase\u price=Property.remove\u currency\u格式(self.purchase\u price\u-before\u-type\u-cast)
结束
结束
#
#处理从货币对象中删除所有字符
#除了0-9和。
#
定义self.remove_currency_格式(currency_属性)
货币属性.gsub(/[^0-9.]/,“”)
结束
结束
租金等级:
class Rent < ActiveRecord::Base
belongs_to :property
before_save :handle_before_save
def handle_before_save
remove_currency_formatting
end
def remove_currency_formatting
if self.current_price.to_s != self.current_price_before_type_cast.to_s
self.current_price = Property.remove_currency_format(self.current_price_before_type_cast)
end
if self.market_price.to_s != self.market_price_before_type_cast.to_s
self.market_price = Property.remove_currency_format(self.market_price_before_type_cast)
end
end
end
classrent
不确定我是否看到了bug或遗漏了一些明显的内容。
谢谢你调查这件事
更新
在我发布这篇文章后,我发现了这个问题,帮助我解决了这个问题。在我看来,我最初的问题仍然是一个bug
我能够将我的代码简化为以下内容,并且一切正常
class Property < ActiveRecord::Base
before_save :handle_before_save
has_many :rents, :dependent => :destroy
accepts_nested_attributes_for :rents, :allow_destroy => true
def handle_before_save
if new_record?
generate_default_rent_data
end
end
def purchase_price=(data)
if data.is_a?(String)
data = Property.remove_currency_format(data)
write_attribute(:purchase_price, data)
end
end
def capital_reserves=(data)
if data.is_a?(String)
data = Property.remove_currency_format(data)
write_attribute(:capital_reserves, data)
end
end
#
# generate some default data
#
def generate_default_rent_data
10.times do |i|
self.rents.build(:apartment_number => i+1) # provide a default value for apartment numbers
end
end
def self.remove_currency_formatting(data)
if data.is_a?(String)
data = Property.remove_currency_format(data)
end
return data
end
#
# handles removing all characters from currency objects
# except for 0-9 and .
#
def self.remove_currency_format(currency_attribute)
currency_attribute.gsub(/[^0-9.]/, "")
end
def purchase_price=(data)
_write_attribute(:purchase_price, data)
end
def capital_reserves=(data)
_write_attribute(:capital_reserves, data)
end
private
def _write_attribute(attribute, data)
write_attribute(attribute, Property.remove_currency_formatting(data))
end
end
class Rent < ActiveRecord::Base
belongs_to :property
def current_price=(data)
_write_attribute(:current_price, data)
end
def market_price=(data)
_write_attribute(:market_price, data)
end
private
def _write_attribute(attribute, data)
write_attribute(attribute, Property.remove_currency_formatting(data))
end
end
class属性:破坏
接受\u嵌套的\u属性\u for:rents,:allow\u destroy=>true
保存前的def句柄
如果有新的记录?
生成默认租金数据
结束
结束
def采购价格=(数据)
if data.is_是?(字符串)
数据=属性。删除\u货币\u格式(数据)
写入属性(:采购价格,数据)
结束
结束
def资本储备=(数据)
if data.is_是?(字符串)
数据=属性。删除\u货币\u格式(数据)
写入属性(:资本准备金、数据)
结束
结束
#
#生成一些默认数据
#
def生成默认租金数据
10.3倍于我|
self.rents.build(:公寓号=>i+1)#为公寓号提供默认值
结束
结束
定义自身。删除\u货币\u格式(数据)
if data.is_是?(字符串)
数据=属性。删除\u货币\u格式(数据)
结束
返回数据
结束
#
#处理从货币对象中删除所有字符
#除了0-9和。
#
定义self.remove_currency_格式(currency_属性)
货币属性.gsub(/[^0-9.]/,“”)
结束
def采购价格=(数据)
_写入属性(:采购价格,数据)
结束
def资本储备=(数据)
_写入属性(:资本准备金、数据)
结束
私有的
定义写入属性(属性、数据)
写入属性(属性,属性。删除货币格式(数据))
结束
结束
类租金
这可能会很有帮助,如果您选择使用它,可能会清理很多write\u属性代码
在我们的应用程序中,我们经常遇到这个问题,我们有大量的金额字段,这将是
ActiveRecord::ConnectionAdapters::Column.class_eval do
def type_cast_with_commas_removed(value)
if type == :decimal && value.is_a?(String)
value = value.delete(',')
end
type_cast_without_commas_removed(value)
end
alias_method_chain :type_cast, :commas_removed
end
class BigDecimal
alias :old_to_s :to_s
def to_s(s=nil)
if s.nil?
parts = ("%.2f" % self).split('.')
parts[0].gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1,")
parts.join('.')
else
old_to_s(s)
end
end
end