让 Stripe 在 Rails 4 中工作
Getting Stripe working in Rails 4
我正在尝试在 rails 中设置条带,我可以输入信用卡信息,但是当它重定向时我收到以下错误:
No route matches [POST] "/enrollment/1"
我很困惑,因为当我收集路线时,我得到:
enrollment_index GET /enrollment(.:format) enrollment#index
POST /enrollment(.:format) enrollment#create
new_enrollment GET /enrollment/new(.:format) enrollment#new
edit_enrollment GET /enrollment/:id/edit(.:format) enrollment#edit
enrollment GET /enrollment/:id(.:format) enrollment#show
PATCH /enrollment/:id(.:format) enrollment#update
PUT /enrollment/:id(.:format) enrollment#update
DELETE /enrollment/:id(.:format) enrollment#destroy
所以我很困惑为什么要尝试 post 因为在我看来,我指定应该放置该方法:
<div class='container'>
<div class='jumbotron enroll-page'>
<div class='row'>
<h1>Enroll Here</h1>
<%= form_tag enrollment_path(1), :method => :put do %>
<article>
<label class="amount">
<span>Amount: .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"></script>
<% end %>
</div>
</div>
</div>
我的控制器是这样的:
class EnrollmentController < ApplicationController
before_filter :authenticate_user!, unless: :admin_signed_in?
def new
end
def create
#get credit card details submitted by form
token = params[:stripeToken]
customer = Stripe::Customer.create(
:card => token,
:plan => 3455,
:email => current_user.email
)
current_user.paid = true
current_user.stripeid = customer.id
current_user.save
redirect_to courses_path, :notice => 'Your enrollment is complete!'
end
end
非常感谢您提供的任何帮助!
编辑:我想我知道了。 Ajay 完全正确,如果我将控制器命名为 "enrollments" 而不是注册,效果会更好。在 new.html.erb 视图中,它应该看起来像这样:
<div class='container'>
<div class='jumbotron enroll-page'>
<div class='row'>
<h1>Enroll Here</h1>
<%= form_tag enrollments_path do %>
<article>
<label class="amount">
<span>Amount: .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"></script>
<% end %>
</div>
</div>
</div>
签入路线资源必须是注册而不是注册
#routes.rb
resources :enrollments
现在当你运行
rake routes | grep 'enrollment'
new_enrollment GET /enrollments/new(.:format) enrollments#new
#app/controller/enrollments_controller.rb
class EnrollmentsController < ApplicationController
def new
@enrollment = Enrollment.new
end
def edit
@enrollment = Enrollment.where(id: params[:id]).first
end
def create
#catch your params and do whatever.
end
def update
# catch your params and do your operation.
end
end
视图来了:
app/views/enrollments/new.html.erb
<%= render 'form %>
app/views/enrollments/edit.html.erb
<%= render 'form' %>
现在在你的 app/views/enrollments/_form.html.erb
<%= form_tag(@enrollment)do %>
<%= label_tag :amount %>
<%= text_field_tag :amount, 50.00 %>
<%= submit_tag %>
<% end %>
在您的控制台中,日志应该是这样的:
Started GET "/enrollments/new" for 127.0.0.1 at 2015-01-10 06:18:08 +0530
Processing by EnrollmentsController#new as HTML
这确认您正在访问新页面而不是编辑页面。核实 !
检查你的表格,它应该是这样的,检查一下。
我正在尝试在 rails 中设置条带,我可以输入信用卡信息,但是当它重定向时我收到以下错误:
No route matches [POST] "/enrollment/1"
我很困惑,因为当我收集路线时,我得到:
enrollment_index GET /enrollment(.:format) enrollment#index
POST /enrollment(.:format) enrollment#create
new_enrollment GET /enrollment/new(.:format) enrollment#new
edit_enrollment GET /enrollment/:id/edit(.:format) enrollment#edit
enrollment GET /enrollment/:id(.:format) enrollment#show
PATCH /enrollment/:id(.:format) enrollment#update
PUT /enrollment/:id(.:format) enrollment#update
DELETE /enrollment/:id(.:format) enrollment#destroy
所以我很困惑为什么要尝试 post 因为在我看来,我指定应该放置该方法:
<div class='container'>
<div class='jumbotron enroll-page'>
<div class='row'>
<h1>Enroll Here</h1>
<%= form_tag enrollment_path(1), :method => :put do %>
<article>
<label class="amount">
<span>Amount: .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"></script>
<% end %>
</div>
</div>
</div>
我的控制器是这样的:
class EnrollmentController < ApplicationController
before_filter :authenticate_user!, unless: :admin_signed_in?
def new
end
def create
#get credit card details submitted by form
token = params[:stripeToken]
customer = Stripe::Customer.create(
:card => token,
:plan => 3455,
:email => current_user.email
)
current_user.paid = true
current_user.stripeid = customer.id
current_user.save
redirect_to courses_path, :notice => 'Your enrollment is complete!'
end
end
非常感谢您提供的任何帮助!
编辑:我想我知道了。 Ajay 完全正确,如果我将控制器命名为 "enrollments" 而不是注册,效果会更好。在 new.html.erb 视图中,它应该看起来像这样:
<div class='container'>
<div class='jumbotron enroll-page'>
<div class='row'>
<h1>Enroll Here</h1>
<%= form_tag enrollments_path do %>
<article>
<label class="amount">
<span>Amount: .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"></script>
<% end %>
</div>
</div>
</div>
签入路线资源必须是注册而不是注册
#routes.rb
resources :enrollments
现在当你运行
rake routes | grep 'enrollment'
new_enrollment GET /enrollments/new(.:format) enrollments#new
#app/controller/enrollments_controller.rb
class EnrollmentsController < ApplicationController
def new
@enrollment = Enrollment.new
end
def edit
@enrollment = Enrollment.where(id: params[:id]).first
end
def create
#catch your params and do whatever.
end
def update
# catch your params and do your operation.
end
end
视图来了:
app/views/enrollments/new.html.erb
<%= render 'form %>
app/views/enrollments/edit.html.erb
<%= render 'form' %>
现在在你的 app/views/enrollments/_form.html.erb
<%= form_tag(@enrollment)do %>
<%= label_tag :amount %>
<%= text_field_tag :amount, 50.00 %>
<%= submit_tag %>
<% end %>
在您的控制台中,日志应该是这样的:
Started GET "/enrollments/new" for 127.0.0.1 at 2015-01-10 06:18:08 +0530
Processing by EnrollmentsController#new as HTML
这确认您正在访问新页面而不是编辑页面。核实 !
检查你的表格,它应该是这样的,检查一下。