我有要保存到我的Stripe_account表的表单。我最近嵌套了资源,现在该表单无法保存到我的数据库表中。我仍然可以使用Stripe API并在那里工作。
我的代码中缺少什么?
用户模型:
has_one :stripe_account
Stripe_account模型:
belongs_to :users
Stripe_account控制器:
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
查看:
<%= form_for ([@user, @stripe_account]) do | f | %>
路线:
resources :users do
resources :stripe_accounts
end
#added for testing
get 'stripe_' => "stripe_account#create"
get 'stripe_new' => "stripe_account#new"
这可能对我的路线有帮助吗?:https://pastebin.com/RVWd2Qq9
现在,即使我没有正确设置“ bankaccount”控制器或模型,它是否也至少应该尝试去那里并保存stripe_account?只要确保这不是问题。但似乎失败了,因为重新加载了新表单。
API也成功通过,并且帐户显示在条带中,而不是我自己的数据库中。
我的编程中有什么问题吗?
更新以添加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)
答案 0 :(得分:1)
验证失败:用户必须存在
您可以使用optional :true
来解决错误
#stipe_account.rb
belongs_to :user, optional: true
#^ should be singular
OR
像这样在user_id
动作中分配create
@stripe_account.user_id = current_user.id #add this line
更新:
StripeAccount:0x001234f4c58692ae8的未定义方法`user_id =' 你的意思是?用户=“
该错误是因为您在user_id
表中没有stripe_accounts
列。生成将为您完成工作的迁移
rails g migration add_user_id_to_stripe_accounts user_id:integer
然后执行rails db:migrate