单个Ruby on Rails表单中的基本和付费注册

时间:2013-06-18 08:16:56

标签: ruby-on-rails paypal paypal-sandbox

我是ruby on rails的新手。 我正在尝试在具有BasicPaid用户单选按钮的轨道上的ruby上设计注册表单。当用户点击付费并点击提交时,应发生以下情况:1)重定向到PayPal进行付款。 2)付款确认后将用户保存到DB(postGres)。

对于基本用户,应创建用户。目前我已成功创建并测试了Basic的scneario。这是我的 的 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>

这是我的 conrtoller 代码。

    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

请告诉我实施付费方案的最佳方法是什么?在此先感谢

1 个答案:

答案 0 :(得分:1)

您可以在创建操作中执行此操作:

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