Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么不是';我的表单不保存,但只使用API吗?_Ruby On Rails_Ruby_Stripe Payments - Fatal编程技术网

Ruby on rails 为什么不是';我的表单不保存,但只使用API吗?

Ruby on rails 为什么不是';我的表单不保存,但只使用API吗?,ruby-on-rails,ruby,stripe-payments,Ruby On Rails,Ruby,Stripe Payments,我有我的表格要保存到我的帐户表中。我最近嵌套了这些资源,现在表单不会保存到我的数据库表中。我仍然在使用StripeAPI,而且还在那里工作 我的代码中缺少什么 用户模型: has_one :stripe_account 客户模式: belongs_to :users 客户控制员: def new @stripe_account = StripeAccount.new @user = User.find(params[:user_id])

我有我的表格要保存到我的帐户表中。我最近嵌套了这些资源,现在表单不会保存到我的数据库表中。我仍然在使用StripeAPI,而且还在那里工作

我的代码中缺少什么

用户模型:

  has_one :stripe_account
客户模式:

  belongs_to :users
客户控制员:

    def new
        @stripe_account = StripeAccount.new
        @user = User.find(params[:user_id])

      end

    def create

        @stripe_account = StripeAccount.new(stripe_account_params)
        @user = User.find(params[:user_id])
          acct = Stripe::Account.create({
.....
.....
    @stripe_account.id = current_user.id
    @stripe_account.acct_id = acct.id

  respond_to do |format|

      # @user = User.find(params[:id])

      if @stripe_account.save
        # current_user = @user
        @user.stripe_account = acct.id


        format.html { redirect_to new_bank_account_path, notice: 'Stripe account was successfully created.' }
        format.json { render :show, status: :created, location: @stripe_account }


      else
        format.html { render :new }
        format.json { render json: @stripe_account.errors, status: :unprocessable_entity }
      end
    end
  end
视图:

这是我的路线也许能帮上忙

现在,即使我还没有正确设置“bankaccount”控制器或模型,它是否至少应该尝试去那里保存stripe_帐户?只是确保这不是问题所在。但它似乎失败了,因为重新加载了一个新表单

API也成功通过了,帐户出现在stripe中,而不是我自己的数据库中

我的程序有什么问题

更新以添加cmd响应:

Started POST "/users/2/stripe_accounts" for 127.0.0.1 at 2018-11-10 00:11:26 -0500
Processing by StripeAccountsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"nz1234567890iJuFwsm/Z4ylhE6zoGdWN6QCfWtDZTH1sxZu/WCdWMKBGkc4zoZ2dOgk9c8UDwRzgqdxrT/sA==", "stripe_account"=>{"account_type"=>"individual", "business_name"=>"", "business_tax_id"=>"", "first_name"=>"Dill", "last_name"=>"Pickles", "ssn_last_4"=>"1234", "dob_month"=>"3", "dob_day"=>"4", "dob_year"=>"1917", "address_line1"=>"198 berry avenue", "address_city"=>"san fran", "address_state"=>"CA", "address_postal"=>"90213", "tos"=>"1", "id"=>"2"}, "full_account"=>"{:value=>\"true\"}", "button"=>"", "user_id"=>"2"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ /home/bob/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/stripe_accounts_controller.rb:49
   (0.1ms)  begin transaction
  ↳ app/controllers/stripe_accounts_controller.rb:91
   (0.1ms)  rollback transaction
  ↳ app/controllers/stripe_accounts_controller.rb:91
  Rendering stripe_accounts/new.html.erb within layouts/application
  Rendered stripe_accounts/_account_form.html.erb (9.4ms)
  Rendered stripe_accounts/new.html.erb within layouts/application (12.5ms)
  Rendered layouts/_navbar.html.erb (1.9ms)
  Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 3202ms (Views: 190.0ms | ActiveRecord: 2.4ms)
验证失败:用户必须存在

您可以使用
可选:true
来解决错误

#stipe_account.rb
belongs_to :user, optional: true
            #^ should be singular

create
操作中分配
user\u id
,如下所示

@stripe_account.user_id = current_user.id #add this line
更新:

StripeAccount:0x001234f4c58692ae8的未定义方法'user_id=' 你是说?用户=”

错误是因为在
stripe\u accounts
表中没有
user\u id
列。请生成一个将为您完成此工作的迁移

rails g migration add_user_id_to_stripe_accounts user_id:integer

你能用表单提交时出现的日志更新这个问题吗?谢谢你的回答。在表单提交后,刚刚从cmd中添加了日志。你可以回滚。尝试将
if@stripe\u account.save
更改为
if@stripe\u account.save!
这样它会显示所有验证错误f当我有“属于:用户”时,我现在得到“(验证失败:用户必须存在)”,当我有“属于:用户”时,我得到(验证失败:用户必须存在)…我的模型错了吗?两者都错了吗?因为我已经有“@stripe\u account.id=current\u User.id”“在创建操作的顶部。。这是正确的位置还是应该在条带创建部分之后?@uno
@stripe\u account.id=current\u user.id
实际上是错误的!将其从创建操作中删除。您应该改为使用
@stripe\u account.user\u id=current\u user.id
。使用“@stripe\u account.id=current\u user.id”我得到以下错误:SQLite3::ConstraintException:唯一约束失败。。。使用user_id,我得到:未定义的方法'user_id='for#你的意思是什么?user=“@uno您应该在
stripe\u accounts
表中有
user\u id
。创建一个将为您完成任务的迁移。@uno您不应该为
id
分配任何自定义值。它是在后台自动生成的主键。顺便说一句,检查我更新的答案。
@stripe_account.user_id = current_user.id #add this line
rails g migration add_user_id_to_stripe_accounts user_id:integer