我是ruby on rails的新手。我正试图在rails上设计一个注册表单,该表单具有Basic
和Paid
用户的单选按钮。当用户点击"付费"并点击"提交"时,应该发生以下情况:1)重定向到PayPal进行支付。2)付款确认后将用户保存到DB(postGres)。
对于基本用户,需要创建User。目前我已经成功地创建并测试了Basic的场景。这是我的。 new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<label for="theme">User Type:</label>
<% [ 'Basic', 'Paid' ].each do |theme| %>
<%= radio_button_tag 'theme', theme, @theme == theme %>
<%= theme.humanize %>
<% end %>
<br/>
<br/>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
这是我的控制器代码。
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to Product Recall!"
redirect_to @user
else
render 'new'
end
end
请让我知道实施付费方案的最佳方式是什么?
您可以在创建操作中设置:
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
if params[:theme] == 'Paid'
redirect_to paypal_payment_path #This could be your another action in your controller where you may need to initiate something and redirect to paypal
return
else
flash[:success] = "Welcome to Product Recall!"
redirect_to @user
end
else
render 'new'
end
end