【问题标题】:Object.attribute_changed? not working in Rails controllerObject.attribute_changed?不在 Rails 控制器中工作
【发布时间】:2012-04-25 00:23:51
【问题描述】:

我正在覆盖设计用户的注册控制器以检查用户是否更改了城市,如果是,则在会话中再次加载 Geokit 对象。

session[:geo_location] = User.geocode(resource.city) if resource.city_changed?

问题是不这样做。下面的代码不会返回任何内容。如果我删除if resource.city_changed?,无论资源是否已更改,它都适用于所有情况。

 User::RegistrationsController < Devise::RegistrationsController

  def update 
   self.resource = resource_class.to_adapter.get!(send(:"current_#    {resource_name}").to_key)    
    if resource.update_with_password(params[resource_name])

      session[:geo_location] = User.geocode(resource.city) if resource.city_changed?

      set_flash_message :notice, :updated if is_navigational_format?
      sign_in resource_name, resource, :bypass => true
      respond_with resource, :location => after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      respond_with_navigational(resource){ render_with_scope :edit }
    end

  end

end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 devise actioncontroller


    【解决方案1】:

    update_with_password 是否调用 save?如果是这样,它会清除模型的“脏”状态及其所有属性。您需要调用resource.city_changed?设置属性但之前调用save

    【讨论】:

    • 请使用update_with_password 方法的代码更新您的原始问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多