我正在尝试将应用程序迁移到rails 4和ruby2.
在我的模型中Product,我有这个:
attr_accessor :name, :age, :price
Run Code Online (Sandbox Code Playgroud)
我切换到:
attr_reader :name, :age, :price
Run Code Online (Sandbox Code Playgroud)
在我看来,我有这个:
%td.small
= number_with_precision(@p.get_last_usd_price.to_f, precision: 2, delimiter: ",", strip_insignificant_zeros: true)
Run Code Online (Sandbox Code Playgroud)
一切都运行良好的rails 3.2和attr_accessor.但现在我遇到了一个问题get_last_usd_price:
def get_last_usd_price
price = 0.0
puts "--------------------"
puts self.inspect #---> prints a #<product> with all the correct attributes
puts
puts self.name.inspect #---> prints nil
puts @name.inspect #---> prints nil
puts "--------------------"
# blah blah
end
Run Code Online (Sandbox Code Playgroud)
基本上,当我尝试访问它们时,所有变量都是nil,但它们被正确保存到对象实例中.
我究竟做错了什么?我怎样才能解决这个问题?
提前致谢.
编辑:控制器
def detail_product
@p = Product.find(params[:product_id]) if params[:product_id].present?
if !@p.present? then
flash[:error] …Run Code Online (Sandbox Code Playgroud)