我的标准current_user.role在我的ChargesController
创建操作中没有更改为premium,即使我的创建(post)操作仍然存在。我的UsersController中的降级操作正常运行,并且使用相同的current_user.role = 'new_role_here'
更改用户角色即可。我很好奇为什么我的current_user.role
没有改变成功的保存信息?任何帮助将不胜感激,谢谢。
ChargesController 使用条带
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
充电/ 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 %>
路由
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]
答案 0 :(得分:2)
我发现,如果我使用current_user.update_attribute(:role, 'premium')
代替current_user.role = 'premium
,那就可以了。