Rails 参数数量错误(1 代表 0),SubscribeController 中的 ArgumentError#update

Rails wrong number of arguments (1 for 0), ArgumentError in SubscribeController#update

我使用 stripe 创建了一个订阅部分。但是我在路由方面遇到了问题。我不确定发生了什么或该怎么做。任何帮助将不胜感激

迁移到用户模型

class AddExtraDetailsToUser < ActiveRecord::Migration
  def change
      add_column :users, :subscribed, :boolean, :default => false
      add_column :users, :stripeid, :string
  end
end

Routes.rb

Rails.application.routes.draw do

    resources :subscribe

    devise_for :users do
        resources :posts 
        resources :products
    end

    get 'users/:id' => 'users#show', as: :user

end

Subscribe.rb

class SubscribeController < ApplicationController
    before_action :authenticate_user!


    def new
    end

    def update
        token = params [:stripeToken]
        customer = Stripe::Customer.create(
            :card => token,
            :plan => 2,
            :email => current_user.email               
        )

        current_user.subscribed = true
        current_user.stripeid = customer.id
        current_user.save

        redirect_to current_user_path, :notice => "Subscription created"
    end    
end

subscribe.html.erb

<%= form_tag subscribe_path(1), :method => :put do %>

<article>

    <label class="amount">
        <span> Amount: </span>

    </label>

</article>



  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
    data-amount="2700"
    data-name="Test"
    data-description="Subscription"
    data-email="<%= current_user.email %>">
  </script>
</form>

<% end %>

错误:

如果您阅读 params 方法的文档,您会发现它不接受任何参数。但是你写成:

params [:stripeToken]

Ruby 认为您将符号数组 [:stripeToken] 作为参数传递给 params 方法。根据 params 方法签名,它不应该接受任何参数,但是你传递给它,它会说你 -

wrong number of arguments (1 for 0)

应该如下所示:

token = params[:stripeToken]

现在在上面的行中 params returns 一个新的 ActionController::Parameters 对象已经用 request.parameters 实例化了,在返回的对象上你调用了方法 [].