param缺失或创建时值为空

时间:2014-08-03 13:56:42

标签: ruby-on-rails ruby-on-rails-4 devise stripe-payments pundit

我在用户和订阅模型之间建立了一对一的关系,使用Stripe进行付款,设计用户身份验证,Pundit用于角色。

当我从创建操作创建订阅模型时,出现param is missing or the value is empty错误,因为我的表单未传递@subscription对象。为什么我的@subscription没有通过参数传递?

这是我的SubscriptionsContrller代码:

class SubscriptionsController < ApplicationController
  before_filter :authenticate_user!
  after_action :verify_authorized

 # def index
 #  end
 #
 #  def show
 #  end

  def new
    @subscription = Subscription.new
    authorize @subscription
  end

  def edit
  end

  def create
    # create the customer first if it doesn't exist
    if (current_user.customer_id.blank?)
      customer = Stripe::Customer.create(:email => current_user.email, :card => params[:stripeToken])
      current_user.customer_id = customer.id
      current_user.save!
    end

    @subscription = current_user.create_subscription(secure_params)
    @subscription.save_with_payment
    authorize @subscription
  end

  def secure_params
    params.require(:subscription).permit(:stripeToken, :plan_id)
  end

end

这是订阅模型:

class Subscription < ActiveRecord::Base
  belongs_to :user

  def save_with_payment
    if valid?
      # first get the customer id from the subscriber
      customer = Stripe::Customer.retrieve(user.customer_id)

      if (@subscription_id.blank?)
        stripe_sub = customer.subscriptions.create(:plan => plan_id)
        @subscription_id = stripe_sub.id
      else
        stripe_sub = customer.subscriptions.retrieve(subscription_id)
        stripe_sub.plan = plan_id
        stripe_sub.save
      end

      @plan_id = plan_id
      save
    end
  end

end

以下是new.html.erb中的表单代码:

<%= form_for @subscription do |f| %>
  <% if @subscription.errors.any? %>
    <div class="error_messages">
      <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>
    </div>
  <% end %>
  <div class="form-row">
      <%= f.label :plan_id, "Plan " %>
      <%= text_field_tag :plan_id, nil %>
  </div>
  <div class="form-row">
      <%= f.label :card_number, "Credit Card Number " %>
      <%= text_field_tag :card_number, nil, {'data-stripe' => 'number'} %>
  </div>
  <div class="form-row">
      <%= f.label :card_code, "Security Code on Card (CVV)" %>
      <%= text_field_tag :card_code, nil, {'data-stripe' => 'ccv'} %>
  </div>
  <div class="form-row">
    <%= f.label :card_month, "Card Expiration" %>
    <%= select_month nil, {add_month_numbers_true: true}, {name: nil, id: "card_month", 'data-stripe' => 'exp_month'} %>
    <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", 'data-stripe' => 'exp_year'} %>
  </div>
    <div class="payment-errors">
        <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
      </div>
  <div class="actions"><%= f.submit "Subscribe" %></div>
<% end %>

最后,这是我的请求参数

{"utf8"=>"✓", "authenticity_token"=>"d5tcFSiFw1XIcPou3HTrISSOIObujo0XfUq+MTNihuk=", 
"plan_id"=>"basic_yearly", "card_number"=>"4242 4242 4242 4242", "card_code"=>"123", 
"action"=>"create", "controller"=>"subscriptions"}

1 个答案:

答案 0 :(得分:1)

原来我混合并匹配form_for帮助器和表单标签。

为了让它运转起来,请转一下:

<%= text_field_tag :plan_id, nil %>

进入

<%= f.text_field :plan_id %>