簡體   English   中英

在Rails 4中更新定制字段以設計用戶模型

[英]Updating custom field for devise user model in Rails 4

我的收費控制器中有一個來自條帶響應的變量,需要將其存儲在devise用戶對象的customer_id字段中。 我已將遷移添加到customer_id的用戶模型中。 我正在嘗試使用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<--

customer_id在您的數據庫中應該是一個字符串,而不是整數,這似乎是基於您看到的,始終將其設置為0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM