我是 Rails 的新手,所以请原谅我。我知道我收到此错误是因为我指定了参数所需的数量。如果我不这样做,我最终会得到一个只有一个 transaction_id 而其他所有字段都为 nil 的交易条目。我相信我的问题必须处理我的路线,但我不确定出了什么问题。
我的错误:
ActionController::ParameterMissing in TransactionsController#create
param is missing or the value is empty: amount
但我可以看到正在传递字段:
Parameters:
{"utf8"=>"✓","authenticity_token"=>
"H+Xxpze7HWC9oduZC2CSxnWHztfhabEMRCoYy0Gw7upNknF2aIHsZ1G/xQOsICTVd7n4btqtT5760UL7QovIrA==",
"transaction"=>{"amount"=>"29", "transactionType"=>"withdraw"}, "commit"=>"Create Transaction"}
我的交易 Controller :
class TransactionsController < ApplicationController
def new
@account = Account.find(params[:account_id])
@transaction = Transaction.new
@transaction.account_id = @account.id
end
def create
@transaction = Transaction.new(transactions_params)
if @transaction.save
redirect_to '/viewAccount/:id'
else
render 'new'
end
end
private
def transactions_params
params.require(:amount).permit(:account_id, :transactionType)
end
end
我的新 View (表单):
<div class="destination">
<div class="container">
<div class = "form">
<h3>Add a new transaction for <%= current_user.email %> </h3>
<%= form_for(@transaction) do |f| %>
<%= f.text_field :amount %>
<%= f.select :transactionType, [['withdraw', 'withdraw'], ['deposit', 'deposit']] %>
<%= f.submit "Create Transaction", class: "btn-submit" %>
<% end %>
</div>
</div>
</div>
我的路线:
Rails.application.routes.draw do
root 'mysite#index'
get 'signup' => 'users#new'
post 'signup' => 'users#create'
resources :users
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
get'newAccount' => 'accounts#new'
get 'viewAccount/:id' => 'banksite#show'
delete 'logout' => 'sessions#destroy'
get 'transactions' => 'transactions#new'
post 'transactions' => "transactions#create"
resources :transactions
银行业务查看/开始交易新
<div class=content>
<div class=main>
<h1>Welcome to Online Banking <%= current_user.email %></h1>
<p>You're only allowed to see this page if you are logged in</p>
<h2><%= current_user.email %> 's Account Summary</h2>
<h3>Balance: $ <%= @account.balance %> </h3>
<%= link_to "Create Transaction",
:controller => "transactions",
:action => "new",
:account_id => @account.id %>
</div>
</div>
</div>
我的模型:
class User < ActiveRecord::Base
has_secure_password
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :user
has_many :transactions
end
class Transaction < ActiveRecord::Base
belongs_to :account
end
如能深入了解此错误,我将不胜感激。
最佳答案
这个:
params.require(:amount).permit(:account_id, :transactionType)
期望参数如下:
"amount" => { "account_id" => "...", "transactionType" => "..." }
但是你的参数看起来像:
"transaction"=>{"amount"=>"29", "transactionType"=>"withdraw"}
此外,您的表单中没有account_id
。
首先,您需要将 account_id
添加到您的表单中,例如:
<%= f.hidden_field :account_id %>
然后你需要在你的 Controller 中更新你的表单解析:
params.require(:transaction).permit(:account_id, :amount, :transactionType)
这样它将在顶层寻找 “transaction”
和 “account_id”
、“amount”
和 “transactionType”
在里面。
关于ruby-on-rails - 表单提交的值为 nil(缺少参数或值为空 : amount),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43702721/