所以我找到了办法,我找到了
resource.update_without_password
如果目标是更新某些用户属性。但我正在尝试更新将发送到Chargify的订阅信息(如信用卡详细信息)。
我该怎么做?
相关代码:
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
def update
super
if params[:product]
if resource.create_and_subscribe(params[:product])
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
subscription = resource.subscription
if subscription
SubscriptionMailer.signup_success(subscription).deliver
end
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
end
end
end
,
class User < ActiveRecord::Base
def create_and_subscribe(product_handle)
if subscription_status != :active
return unless self.valid?
product = Chargify::Product.find_by_handle(product_handle)
if product.nil?
errors.add(:base, "Product must be selected")
return nil
end
User.transaction do
self.save
subscription =
subscription_service.
subscribe(self, subscription_params(product.id))
if subscription.errors.any?
subscription.errors.full_messages.each{|err| errors.add(:base, err)}
false
raise ActiveRecord::Rollback
else
self.subscription_status = subscription.state
self.product = subscription.product.handle
self.save(validate: false)
true
end
end
else
errors.add(:base, "You already have a subscription")
end
end
end
,
class ChargifySubscriptionService
def subscribe(user, params)
record = ChargifyRecord.new
record.user_id = user.id
subscription = Chargify::Subscription.new(params)
record.request_params = params
record.created_at = Time.now
record.status = subscription.save ? :success : :failure
record.responded_at = Time.now
record.response = subscription.to_json
record.save
subscription
end
答案 0 :(得分:0)
这就是我设置用户模型以更新用户密码
的方法#app/models/user.rb
attr_accessor :updating_password
def should_validate_password?
updating_password || new_record?
end
现在在我的控制器中,我会检查密码是否已提交
#app/controllers/users/users_controller.rb
before_filter :check_password_submitted, :only => :update
def check_password_submitted
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
user.updating_password = false
else
user.updating_password = true
end
end
我希望这会有所帮助 和快乐编码