I have the following in my 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 I have the following in my 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 for saving the temperature values. However, there’s one issue. Normally on the edit screen, the select helper would have the existing values pre-selected. But using the virtual attributes this isn’t happening.
How do I make sure that the temp_int and temp_dec values get passed to the view?
The view should have no problem accessing the attributes, but have the attributes been initialized within the view’s action? It looks like, if you’ve just loaded an instance of the model, those attributes are still nil, regardless of the value of
#temp.Note that if your action redirects, the browser will make a new connection to access the subsequent action, and will not have access to the model instance that was use in the original action.
Perhaps, you need to add something like the following: