如何将param值添加到单选按钮标记

时间:2015-01-23 04:30:38

标签: ruby-on-rails

我有一份订阅表格。我把所有信息都移到了一页。以前,我将用户链接到计划/subscriptions/new?plan_id=1/subscriptions/new?plan_id=12的两个单独页面。

现在页面只有subscriptions/new。如何将params添加到每个无线电值,以便当用户使用他们的信用卡或PayPal签出时,他们会使用单选按钮为他们选择的订阅计划签名?

  <%= form_for @subscription do |f| %>
  <div class="col-3">
    <div class="plan">
      <%= image_tag "/assets/best1.png" %>

      <h1><%= radio_button_tag :plan_id, '1,', true %><label for="plan_id_1"><span><span></span></span></label>$13.50 a year</h1>
      <h2>12-Month Membership</h2>
    </div>
    <div class="plan">
      <h1><%= radio_button_tag :plan_id, '2' %><label for="plan_id_2"><span><span></span></span></label>$1.35 a month</h1>
      <h2>1-Month Membership</h2>
    </div>
  </div>
  <div class="sm_form_container">
    <div class="row">
      <div class="field">
        <h1>Payment Information</h1>
     </div>
    </div> 
    <div class="row">
      <div class="field">

      <% if @subscription.errors.any? %>

          <h2><%= pluralize(@subscription.errors.count, "error") %> prohibited this subscription from being saved:</h2>
          <ul>
          <% @subscription.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>

      <% end %>

      <%= f.hidden_field :plan_id %>
      <%= f.hidden_field :stripe_card_token %>
      <%= f.hidden_field :paypal_customer_token %>
      <%= f.hidden_field :paypal_payment_token %>

      <% unless @subscription.payment_provided? %>

        <%= radio_button_tag :pay_with, :card, true %>
        <%= label_tag :pay_with_card do %>
          <%= image_tag "visa.png" %>
          <%= image_tag "mastercard.png" %>
          <%= image_tag "discover.png" %>
          <%= image_tag "american_express.png" %>
          <%= image_tag "jcb.png" %>
        <% end %>
        <%= radio_button_tag :pay_with, :paypal %>
        <%= label_tag :pay_with_paypal do %>
          <%= image_tag "paypal.png" %>
        <% end %>
      <% end %>
      </div>
    </div>
    <div class="row">
      <div id="paypal_checkout" style="display:none">
        <%= link_to image_tag("https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"), paypal_checkout_path(plan_id: @subscription.plan_id) %>
      </div>
    </div>
    <div id="billing_fields">
      <div class="field">
          <%= f.hidden_field :user_id, :value => current_user.id %> 
          <%= f.hidden_field :email, :value => current_user.email  %>
        </div>
        <% if @subscription.payment_provided? %>
          Payment has been provided. Click "Subscribe" to complete the subscription.
        <% else %>
      <div class="row">
          <div class="field">
            <%= label_tag :card_owner, "Name (as it appears on the card)" %>
            <%= text_field_tag :card_owner, nil, name: nil %>
          </div>
          <div class="field">
            <%= label_tag :card_number, "Credit Card Number" %>
            <%= text_field_tag :card_number, nil, name: nil %>
          </div>
      </div>
      <div class="row">
          <div class="field">
            <%= label_tag :card_code, "Security Code on Card (CVV)" %>
            <%= text_field_tag :card_code, nil, name: nil %>
          </div>
          <div class="field" id="expiration">
            <%= label_tag :card_month, "Card Expiration" %><br />
            <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
            <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
          </div>
        <% end %>
        <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>
      <div class="row">
        <div class="field"><%= f.submit "Subscribe", class: 'btn', style: "width: 40%; margin-left: 165px"  %></div>
      </div>
      </div>
    <% end %>
  </div>
</div>

订阅控制器:

      def new
        plan = Plan.find(1)
        @subscription = plan.subscriptions.build
        if params[:PayerID]
          @subscription.paypal_customer_token = params[:PayerID]
          @subscription.paypal_payment_token = params[:token]
          @subscription.email = @subscription.paypal.checkout_details.email
        end
        render layout: 'new_application'   end

 def paypal_checkout
    plan = Plan.find(params[:plan_id])
    subscription = plan.subscriptions.build
    redirect_to subscription.paypal.checkout_url(
      return_url: new_subscription_url(:plan_id => plan.id),
      cancel_url: root_url
    )
  end

路线:

  resources :subscriptions
  resources :plans
  get 'paypal/checkout', to: 'subscriptions#paypal_checkout'

2 个答案:

答案 0 :(得分:0)

假设您拥有会员资格模型,并且会员资格具有ID和名称(以及其他内容)。订阅具有plan_id(会员计划)。

订阅控制器从数据库加载成员资格。

def edit
  @memberships = Memberships.all
end

在编辑视图中,您使用成员资格填充单选按钮集,将plan_id设置为成员资格ID(我使用haml,这是来自表格布局,但它应该很容易翻译)

%td
  - @memberships.each do |membership|
    = f.radio_button(:plan_id, membership.id)
    = f.label :plan_id, membership.name
    %br

保存时,从params构建的订阅模型将具有所选成员资格模型的plan_id值。

答案 1 :(得分:0)

您可以在new操作中预先填充模型,然后使用表单构建器,以便这些预填充值传播到视图。

<强>控制器:

def new
  if params.has_key?(:plan_id) 
    plan = Plan.find(params[:plan_id])
  else
    # make plan with id 1 our default plan if no plan_id has been given
    plan = Plan.find(1)
  end

  # this should set the plan_id in the Subscription model,
  # if not - ensure you have a `belongs_to :plan` inside your Subscription model.
  @subscription = plan.subscriptions.build
  if params[:PayerID]
    @subscription.paypal_customer_token = params[:PayerID]
    @subscription.paypal_payment_token = params[:token]
    @subscription.email = @subscription.paypal.checkout_details.email
  end
  render layout: 'new_application'   
end

查看:

首先删除隐藏的字段标记<%= f.hidden_field :plan_id %>,然后调整此部分以使用表单构建器,即<%= f.radio_button :plan_id, ... %>。然后,表单构建器将负责选择正确的单选按钮。

  <%= form_for @subscription do |f| %>
  <div class="col-3">
    <div class="plan">
      <%= image_tag "/assets/best1.png" %>

      <h1><%= f.radio_button :plan_id, 1 %><label for="plan_id_1"><span><span></span></span></label>$13.50 a year</h1>
      <h2>12-Month Membership</h2>
    </div>
    <div class="plan">
      <h1><%= f.radio_button :plan_id, 2 %><label for="plan_id_2"><span><span></span></span></label>$1.35 a month</h1>
      <h2>1-Month Membership</h2>
    </div>
  </div>

  ....