如何更新 rails 控制器中的记录

how to update record in rails controller

我正在使用 stripe 供用户订阅。从那里我收集了条纹日期并将其保存在订阅模式中。我为我的用户模型创建了枚举,以根据条带订阅 ID 分配不同的角色。 这是我的用户模型的样子

class User < ApplicationRecord
  enum role: [:user, :gold, :platinum, :diamond ]
  has_one :subscription
  has_one :shipping
  extend FriendlyId
 friendly_id :firstname, use: [:slugged, :finders]

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

end

下面是我的订阅控制器

   def subscription_checkout
     user = current_user
     plan_id = params[:plan_id]
     plan = Stripe::Plan.retrieve(plan_id)
     # This should be created on signup.
     customer = Stripe::Customer.create(
         description: 'Customer for test@example.com',
         source: params[:stripeToken],
         email: 'test@example.com'
     )
     # Save this in your DB and associate with the user;s email
     stripe_subscription = customer.subscriptions.create(plan: plan.id)
     @sub = Subscription.create(plan: stripe_subscription.plan.name,
                                stripeid: stripe_subscription.id,
                                user_id: current_user.id,
                                customer: stripe_subscription.customer,
                                subscriptionenddate: stripe_subscription.current_period_end)
     if @sub.save
         current_user.role  plan.id
         current_user.save
         flash[:success] = 'sub created!'
         redirect_to root_url
     else
         render 'new'
     end
 end

当到达更新角色时,我得到

ArgumentError: wrong number of arguments (1 for 0)

如何更新角色以及我做错了什么?

你试过了吗:current_user.role = plan.id

看起来您只是在 curent_user 对象上调用 role() 方法。