Passing virtual attribute values through to form elements in Rails 3 -
i have following in model:
before_validation :update_temp attr_accessor :temp_int, :temp_dec def update_temp self.temp = temp_int.to_f + (temp_dec.to_f / 10) end and have following in view:
<%= f.select(:temp_int, ["97", "98"], { :include_blank => true })%> . <%= f.select(:temp_dec, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], { :include_blank => true })%> this works great saving temperature values. however, there's 1 issue. on edit screen, select helper have existing values pre-selected. using virtual attributes isn't happening.
how make sure temp_int , temp_dec values passed view?
the view should have no problem accessing attributes, have attributes been initialized within view's action? looks like, if you've loaded instance of model, attributes still nil, regardless of value of #temp.
note if action redirects, browser make new connection access subsequent action, , not have access model instance use in original action.
perhaps, need add following:
after_initialize :init_temp_parts def init_temp_parts return if temp.blank? @temp_int = temp.to_i @temp_dec = temp - @temp_int end
Comments
Post a Comment