我正在使用Devise和CanCanCan处理Rails 4 Web应用程序。
当用户首次注册创建帐户时,会将其重定向到订阅页面,并在其中输入信用卡详细信息等。
用户创建 - >订阅创建=表单工作
这非常完美,一旦他们输入订阅信息,就会对他们的帐户进行权限更改,然后他们就可以访问该网站的新部分。一切都好。
现在我遇到的问题是,如果用户注册,然后尝试访问该网站的新部分而不进行订阅,我会将其重定向到订阅页面。但是,当我这样做时,表格不起作用。他们点击提交,没有任何反应。
用户创建 - >尝试访问资源,重定向到 订阅创建=表格不起作用
以下是我用来执行重定向的代码:
application_controller.rb
def access_denied(exception)
redirect_to(new_subscription_path, alert: exception.message + " Please subscribe.")
end
奇怪的是,我在第一次创建用户帐户时使用完全相同的代码进行重定向。这显示在这里:
registrations_controller.rb
def after_sign_up_path_for(resource)
new_subscription_path
end
以下是订阅控制器的代码:
class SubscriptionsController < ApplicationController
before_filter :authenticate_user!
def new
@subscription = Subscription.new
end
def create
@subscription = Subscription.new(subscription_params)
@user = current_user
@subscription.user_id = current_user.id
if @subscription.save_with_payment
redirect_to success_path, :notice => "Thank you for subscribing!"
if current_user.role = "guest"
User.update(current_user.id, role: "read")
end
UserMailer.subscription_welcome_email(@user).deliver
else
render :new
end
end
def show
@subscription = Subscription.find(params[:id])
end
def destroy
@subscription = Subscription.find_by(user_id: current_user.id)
User.update(current_user.id, role: "guest")
unless @subscription.stripe_customer_id.nil?
customer = Stripe::Customer.retrieve(@subscription.stripe_customer_id)
customer.subscriptions.retrieve(customer.subscriptions.first.id).delete
end
@user = current_user
UserMailer.subscription_destroy_email(@user).deliver
@subscription.destroy
rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "Unable to cancel your subscription. #{e.message}."
false
end
def subscription_params
params.require(:subscription).permit(:stripe_card_token, :last_4_digits, :plan, :expiry_month, :expiry_year)
end
end
表格代码:
<div class='panel panel-default'>
<div class='panel-heading'>
<h2>Subscribe</h2>
</div>
<div class='panel-body'>
<%= semantic_form_for @subscription, :html => {:class => 'main-form'} do |f| %>
<font color=red><b><%= f.semantic_errors *f.object.errors.keys %></b></font>
<%= f.hidden_field :stripe_card_token %>
<div id='stripe_error' class="alert alert-info" style='display:none'>
</div>
<span class="help-block">Nothing is billed to your card for 7 days. <b>Guaranteed. </b>
<br>If you choose to continue after 7 days, only then will you be billed.</span>
<div class='form-group'>
<%= label_tag :card_number, "Credit Card Number" %><%= image_tag "welcome/checkout/amex.png", alt: "American Express", class: "credit-card-image" %><%= image_tag "welcome/checkout/mastercard.png", alt: "Mastercard", class: "credit-card-image" %><%= image_tag "welcome/checkout/visa.png", alt: "Visa", class: "credit-card-image" %>
<%= text_field_tag :card_number, nil, name: nil, class: 'form-control input-box', :placeholder => 'Credit Card Number' %>
</div>
<div class='row'>
<div class="col-xs-6">
<%= label_tag :card_code, "Security Code on Card (CVC)" %><%= image_tag "welcome/checkout/credit.png", alt: "Credit Card Image", class: "credit-card-image" %>
<%= text_field_tag :card_code, nil, name: nil, class: 'form-control input-box', :placeholder => 'Security Code on Card (CVC)' %>
</div>
<div class="col-xs-6">
<%= label_tag :card_month, "Card Expiration" %><br>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month", class: 'expiration'} %>
<%= select_year nil, {start_year: Date.today.year+1, end_year: Date.today.year+15}, {name: nil, id: "card_year", class: 'expiration'} %>
</div>
</div>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<div>
<%= f.submit "Subscribe", class: 'btn standard-button' %>
</div>
<% end %>
</div>
</div>
有人可以帮忙吗?如果需要任何额外的代码,请告诉我。感谢
编辑:只是为了让事情变得奇怪,只有当我点击导航中的链接new_subscription_path时才会发生此错误。如果我手动键入权限资源的URL,则拒绝访问可以正常工作。我在标题中使用的路径是否有问题?我是否需要将特定内容传递给它?我还尝试通过添加控制台日志来检查它是否是JavaScript,但没有任何结果,所以我不认为这是一个问题,尽管它感觉像是一个JS问题。
答案 0 :(得分:0)
你应该检查你的javascript。很明显,它会阻止提交形式。我查看了您之前的问题here,看起来您的handleStripeResponse
处理程序总是进入else
条件阻止而不提交表单作为结果。我敢打赌,重定向后$('meta[name="stripe-key"]').attr('content')
未定义。
答案 1 :(得分:0)
已禁用Turbolinks解决了这个问题