2015-01-23 5 views
1

나는 가입 양식이 있습니다. 그리고 모든 정보를 한 페이지로 옮겼습니다. 이전에 사용자는 /subscriptions/new?plan_id=1 또는 /subscriptions/new?plan_id=12 계획의 두 페이지로 연결되었습니다.라디오 버튼 태그에 param 값을 추가하는 방법

이제 페이지는 subscriptions/new입니다. 각 라디오 값에 매개 변수를 추가하면 사용자가 신용 카드 또는 페이팔을 사용하여 체크 아웃 할 때 라디오 버튼을 사용하여 선택한 가입 계획에 서명 할 수 있습니다.

<%= 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' 
+0

'* _tag'헬퍼를 사용하는 이유가 무엇입니까? '@ subscription'이 모델 인스턴스 인 경우 각각 <% = f.radio_button : plan_id, 1 %>'및'<% = f.radio_button : plan_id, 2 %>'를 수행하여 선택한 값을 'params [: subscription] [: plan_id]' – sled

+0

@sled 컨트롤러를 포함 시켰습니다. 내가'plan = plan.find (1)'을 읽는 것을 잊었다. 그래서 항상 계획 1로 가게된다. 나는 컨트롤러를 plan = find.find (params [: plan_id])로 변경하려고 생각하고 있었다. 내가보기에 합격하면 효과가 있어야한다. 그러나 뷰에 매개 변수를 전달하는 예를 보여줄 수 있습니까? –

답변

0

당신이 회원 모델을 가정하고, 회원이 (다른 것들 사이) ID와 이름이 있습니다. 구독에는 plan_id (회원 계획)가 있습니다.

구독 컨트롤러가 DB에서 구성원 자격을로드합니다.

def edit 
    @memberships = Memberships.all 
end 

그리고 편집보기에서

, 당신은 (내가 HAML을 사용하고, 이것은 테이블 레이아웃에서,하지만 쉽게 번역해야 회원 ID에 plan_id를 설정, 라디오 버튼 세트를 채우기 위해 구성원을 사용 충분 함)

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

저장시 params로 작성된 구독 모델은 선택한 멤버십 모델의 plan_id 값을 갖습니다.

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> 

    .... 
+0

방금 ​​코드를 살펴 보았습니다. 네가 한 것과 다른 것을 못 본다. 그 마지막 게시물을 만들기 전에 나는 그 성공을하기 전에 그 결정을 바꿨다는 뜻입니다. 저는 ID없이 계획을 찾을 수 없었습니다. 그래서 나는보기에 매개 변수를 전달하는 방법을 묻습니다. 나는 코드를 작성하겠습니다. 그러나 문자 그대로 과거에 시도한 것과 똑같습니다. –

+0

동일한 코드를 가지고있는 것과 동일한 오류를 재현하는지 확인하십시오. 'ActiveRecord :: RecordNotFound at/subscriptions/new 'plan = Plan'행에 ID없는 계획을 찾을 수 없습니다. 'find (params [: plan_id])' –

+0

이것은 단순히 URL에 plan_id 매개 변수를 전달하지 않는다는 것을 의미합니다. 경로는 어떻게 생겼습니까? 계획 ID가 전달되지 않았을 때'ID = 1'을 사용하여 제품을 가져 오는 수표를 추가했습니다. – sled

관련 문제