ruby on rails - Calling a method on User in application_helper.rb returns undefined method -
i'm trying write helper method converts floats imperial metric. have following method in application_helper.rb:
module applicationhelper def weight_conversion_factor if user.measurement_units == 'metric' 0.453592 elsif user.measurement_units == 'imperial' 1 end end end if call current_user.measurement_units in view, works great. when try call user.measurement_units in application_helper.rb file, undefined methodmeasurement_units' #`
what missing here? shouldn't able call measurement_units on user within applicatoin_helper? measurement_units field in user table.
thanks!
user class, not instance. use current_user in helper method too:
module applicationhelper def weight_conversion_factor return nil if current_user.nil? if current_user.measurement_units == 'metric' 0.453592 elsif current_user.measurement_units == 'imperial' 1 end end end alternatively, go in user model:
class user < activerecord::base # other code... def weight_conversion_factor if measurement_units == 'metric' 0.453592 elsif measurement_units == 'imperial' 1 else nil end end end
Comments
Post a Comment