Ruby on rails 订阅后更改用户布尔值
我当前有一些用户的子脚本布尔值设置为False 用户创建订阅后,如何更改值? 我已经在下面编写了我的代码……Rails新手请帮助:) 型号Ruby on rails 订阅后更改用户布尔值,ruby-on-rails,ruby,boolean,boolean-expression,Ruby On Rails,Ruby,Boolean,Boolean Expression,我当前有一些用户的子脚本布尔值设置为False 用户创建订阅后,如何更改值? 我已经在下面编写了我的代码……Rails新手请帮助:) 型号 class User < ActiveRecord::Base attr_accessible :name, :username, :email, :subscribed has_one :subscription, :dependent => :destroy end class Subscription < ActiveR
class User < ActiveRecord::Base
attr_accessible :name, :username, :email, :subscribed
has_one :subscription, :dependent => :destroy
end
class Subscription < ActiveRecord::Base
attr_accessible :plan_id, :user_id, :email
belongs_to :plan
belongs_to :user
after_create :subscribed
def subscribed ***This is my current code
if self.user.subscribed == false
self.user.subscribed = true
end
end
def save_with_payment
if valid?
save_with_stripe_payment
end
end
def save_with_stripe_payment
customer = Stripe::Customer.create(card: stripe_card_token, email: email, plan: plan_id, description: "Unlimited Comics")
self.stripe_customer_token = customer.id
save!
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
class SubscriptionsController < ApplicationController
def new
plan = Plan.find(params[:plan_id])
@subscription = plan.subscriptions.build
@subscription.user_id = current_user.id
end
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :success => "Thank you for subscribing!"
else
render :new
end
end
end
如果你提到的关系是正确的
如果您正确地提到了关系请尝试以下操作
after_create :subscribed
def subscribed
self.user.update_attribute(:subscribed, true) unless self.user.subscribed
end
试着跟随
after_create :subscribed
def subscribed
self.user.update_attribute(:subscribed, true) unless self.user.subscribed
end
我可以看到这里的安全漏洞。恶意用户可以为自己以外的其他人创建订阅。我可以在这里看到安全漏洞。恶意用户可以为自己以外的其他人创建订阅。您应该将其从
subscribed
重命名为subscribe
,订阅的
已经是attribute@AmolPujarisubscribe
是用户
的属性,而不是subscribe
的属性,我们正在类subscribe
中编写方法……因此我认为这不会产生问题……尽管我同意你的观点,方法名称必须像update\u user\u subscribed
您应该将其从subscribed
重命名为subscribe
,订阅的
已经是attribute@AmolPujarisubscribe
是用户
的属性,而不是subscribe
的属性,我们正在类subscribe
中编写方法……因此我认为这不会产生问题……尽管我同意你的观点,方法名称必须像update\u user\u subscribed
after_create :subscribed
def subscribed
self.user.update_attribute(:subscribed, true) unless self.user.subscribed
end