表单错误:表单中的第一个参数不能包含nil或为空

时间:2014-11-23 22:16:55

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

我按照本教程设置与Stripe的Devise集成:http://www.jaredrader.com/blog/2013/12/18/a-stripe-integration

我已经成功设置了Stripe,并创建了各种控制器,模型和视图。

但是,表单正在创建ArgumentError in Users::Registrations#new

这是错误代码:

    ArgumentError in Users::Registrations#new
    Showing /home/action/workspace/mediadb/app/views/devise/registrations/new.html.erb where line #62 raised:

    First argument in form cannot contain nil or be empty

表格:

<div class="panel panel-default">
  <div class="panel-heading">
<% if params[:plan] == "2" %>
<h1>Sign up with premium!</h1>
  </div>

  <div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <%= hidden_field_tag 'plan', params[:plan] %>

    <div class="form-group">
      <%= f.label :name %>
      <%= f.text_field :name, autofocus: true, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :email %>
      <%= f.email_field :email, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :password %>
      <%= f.password_field :password, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation, class: "form-control" %>
    </div>

    <h2>Payment</h2>

    <%= f.hidden_field :stripe_card_token %>
      <div class="form-group">
        <%= label_tag :card_number, "Credit Card Number" %>
        <%= text_field_tag :card_number, nil, name: nil, class: "form-control" %>
      </div>
      <div class="form-group">
        <%= label_tag :card_code, "Security Code on Card (CVV)" %>
        <%= text_field_tag :card_code, nil, name: nil, class: "form-control" %>
      </div>
      <div class="form-group">
        <%= label_tag :card_month, "Card Expiration" %>
        <%= 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>
      <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 class="form-group">
      <%= f.submit "Sign up", class: "btn btn-lg btn-success" %>
    </div>
  <% end %>
  </div>
<% else %>
  <h1>Sign up for free</h1>
  </div>
  <div class="panel-body">
    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { id: "free_plan"}) do |f| %>
        <%= devise_error_messages! %>

        <%= hidden_field_tag 'plan', params[:plan] %>

        <div class="form-group">
          <%= f.label :name %>
          <%= f.text_field :name, autofocus: true, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :email %>
          <%= f.email_field :email, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :password %>
          <%= f.password_field :password, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.label :password_confirmation %>
          <%= f.password_field :password_confirmation, class: "form-control" %>
        </div>

        <div class="form-group">
          <%= f.submit "Sign up", class: "btn btn-lg btn-primary" %>
        </div>
      <% end %>
    <% end %>
  </div>
</div>

routes.rb文件:

    Rails.application.routes.draw do
  resources :lists
  resources :publications
  resources :contacts

  devise_for :users, controllers: { registrations: 'users/registrations' }

  devise_scope :user do
    get '/sign_up', to: 'users/registrations#new', as: :sign_up
    get '/sign_in', to: 'devise/sessions#new', as: :sign_in
    get '/:id/edit', to: 'users/registrations#edit', as: :edit
    put 'users/update_plan', :to => 'users/registrations#update_plan'
    put 'users/cancel_plan', :to => 'users/registrations#cancel_plan'
  end

  resources :users, only: [:index, :show]



  # root should always be last
  root to: 'pages#home'
end

注册控制器:

class Users::RegistrationsController < Devise::RegistrationsController

  def new
    unless (params[:plan] == '1' || params[:plan] == '2')
      flash[:notice] = "Please select a plan to sign up."
      redirect_to root_url
    end
  end

  def update_plan
    @user = current_user
    @user.update_attributes(plan_id: params[:plan], email: params[:email], stripe_card_token: params[:user][:stripe_card_token])
      if @user.plan_id == 2
        @user.save_with_payment
        redirect_to edit_user_registration_path, notice: "Updated to premium!"
      else
        flash[:error] = "Unable to update plan."
        render :edit
      end
  end

  def cancel_plan
    @user = current_user
    if @user.cancel_user_plan(params[:customer])
      @user.update_attributes(stripe_customer_token: nil, plan_id: 1)
      flash[:notice] = "Canceled subscription."
      redirect_to edit_user_registration_path
    else
      flash[:error] = "There was an error canceling your subscription. Please notify us."
      render :edit
    end
  end

  private

  def build_resource(*args)
    super
    if params[:plan]
      resource.plan_id = params[:plan]
      if resource.plan_id == 2
        resource.save_with_payment
      else
        resource.save
      end
    end
  end

  def setup
    plans = Plan.all
    plans.each do |plan|
      unless plan.id == 1
        @startup_plan = plan
      end
    end
  end
end

知道什么是错的吗?

1 个答案:

答案 0 :(得分:1)

按以下方式更新Users::RegistrationsController#new

def new
  if (params[:plan] == '1' || params[:plan] == '2')
    super
  else
    flash[:notice] = "Please select a plan to sign up."
    redirect_to root_url
  end
end