2

我正在覆盖设计用户的注册控制器以检查用户是否更改了城市,如果是,则在会话中再次加载 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
4

1 回答 1

4

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

于 2012-04-25T00:40:15.143 回答