在Rails 4中更新设计用户模型的自定义字段

时间:2014-01-04 02:08:39

标签: ruby-on-rails devise ruby-on-rails-4 stripe-payments

我的计费控制器中有一个变量来自条带响应,我需要存储在设计用户对象的customer_id字段中。我已将迁移添加到customer_id的User模型中。我正在尝试使用update_attributes!将stripe_customer_id变量存储在User对象的customer_id字段中的方法。

我已经编辑了我的application_controller以包含我的自定义字段(customer_id)的强参数。我成功地将stripe_customer_id字符串打印到控制台,但是一旦我尝试将该变量传递给customer_id并将其保存到对象,它就不会保存。

charges_controller.rb (影响用户模型的摘录)

stripe_customer_id = customer.id
puts "-->#{stripe_customer_id}<--"          
if @charge.save 
  redirect_to gifts_path
  flash[:notice] = "It worked!"
  @user.update_attributes!(:customer_id => stripe_customer_id)
  puts "-->#{@user.customer_id}<--"
else 
  render 'charges/new'
  flash[:alert] = 'Something went wrong'
end

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :customer_id) }
  end
end

user.rb

class User < ActiveRecord::Base
  has_many :charges
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

终端SQL

-->cus_3FGHqqW7cgnglX<--
SQL (0.6ms)  UPDATE "users" SET "customer_id" = ?, "updated_at" = ? WHERE "users"."id" = 11  [["customer_id", 0], ["updated_at", Sat, 04 Jan 2014 03:45:46 UTC +00:00]]
-->0<--

1 个答案:

答案 0 :(得分:1)

customer_id应该是一个字符串,而不是数据库中的整数,它似乎是基于你看到它始终被设置为0