Ruby on rails 当前用户角色在升级时未更改(条带)
在我的Ruby on rails 当前用户角色在升级时未更改(条带),ruby-on-rails,ruby,stripe-payments,Ruby On Rails,Ruby,Stripe Payments,在我的ChargesControllercreate操作中,我的标准current_user.role没有更改为premium,即使我的create(post)操作继续执行。我的UsersController中的降级操作执行正常,并使用相同的当前用户角色='new\u role\u here'很好地更改了刚刚找到的用户角色。我很好奇为什么我的当前用户角色在成功保存消息后没有改变?任何帮助都将不胜感激,谢谢 充电器控制器使用条带 before_action :require_sign_in de
ChargesController
create操作中,我的标准current_user.role没有更改为premium,即使我的create(post)操作继续执行。我的UsersController中的降级操作执行正常,并使用相同的当前用户角色='new\u role\u here'
很好地更改了刚刚找到的用户角色。我很好奇为什么我的当前用户角色在成功保存消息后没有改变?任何帮助都将不胜感激,谢谢
充电器控制器使用条带
before_action :require_sign_in
def new
@stripe_btn_data = {
key: Rails.configuration.stripe[:publishable_key].to_s,
description: "BigMoney Membership - #{current_user.email}",
amount: 10
}
end
def create
@amount = 500
customer = Stripe::Customer.create(
email: current_user.email,
source: params[:stripeToken]
)
charge = Stripe::Charge.create(
customer: customer.id,
amount: @amount,
description: "BigMoney Membership - #{current_user.email}",
currency: 'usd'
)
current_user.role = 'premium'
if current_user.save!
flash[:notice] = "Thanks for all the money, #{current_user.email}! changed to premium member, #{current_user.role}"
redirect_to wikis_path # or wherever
end
# Stripe will send back CardErrors, with friendly messages
# when something goes wrong.
# This `rescue block` catches and displays those errors.
rescue Stripe::CardError => e
flash[:alert] = e.message
redirect_to new_charge_path
end
end
charges/new.html.erb
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-locale="auto"></script>
<% end %>
我发现如果我使用current\u user.update\u attribute(:role,'premium')
而不是current\u user.role='premium
,我发现如果我使用current\u user.update\u attribute(:role,'premium'),它会起作用。我发现如果我使用current\u user.update\u attribute(:role,'premium')
而不是current_user.role='premium
它起作用了。确保在可能的时候接受你的答案。这是正确的答案,并将帮助其他有此问题的人知道。此外,你还可以因此获得积分和新徽章我会的。必须等待48小时;)尽可能地接受你的答案。这是正确的答案,并将帮助其他有此问题的人知道。此外,你还可以因此获得积分和新徽章我会的。必须等待48小时;)
Rails.application.routes.draw do
get 'charges/create'
get "log_in" => 'sessions#new', :as => "log_in"
get "log_out" => 'sessions#destroy', :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
get "my_account" => "users#show", :as => "my_account"
get 'users/confirm' => 'users#confirm'
root :to => "sessions#new"
resources :users
resources :sessions
resources :wikis
resources :charges, only: [:new, :create]