【问题标题】:Values are nil for form submission (param is missing or the value is empty: amount)表单提交的值为 nil(参数丢失或值为空:数量)
【发布时间】:2017-04-30 03:48:50
【问题描述】:

我是一个新手,所以请原谅我。我知道我收到了这个错误,因为我指定了参数所需的数量。如果我不这样做,我最终会得到一个只有一个 transaction_id 的交易条目,而其他所有字段都为零。我相信我的问题必须处理我的路线,但我不确定出了什么问题。

我的错误:

TransactionsController#create 中的ActionController::ParameterMissing

参数缺失或值为空:数量

但我可以看到字段正在传递:

     Parameters:
      {"utf8"=>"✓","authenticity_token"=>
"H+Xxpze7HWC9oduZC2CSxnWHztfhabEMRCoYy0Gw7upNknF2aIHsZ1G/xQOsICTVd7n4btqtT5760UL7QovIrA==",
"transaction"=>{"amount"=>"29", "transactionType"=>"withdraw"}, "commit"=>"Create Transaction"}

我的交易控制器:

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

我的新视图(表单):

<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

如果能对此错误有任何见解,我将不胜感激。

【问题讨论】:

  • 我相信您的 resources :transactions 正在淘汰您的自定义 post。尝试删除事务中的get 和post,然后只使用resources :transactions 行。该行应该为您创建所有方法。

标签: ruby-on-rails ruby null routes


【解决方案1】:

这个:

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 %>

然后你需要在你的控制器中更新你的表单解析:

params.require(:transaction).permit(:account_id, :amount, :transactionType)

这样它将在顶层查找"transaction",并在其中查找"account_id"、"amount" 和"transactionType"。

【讨论】:

  • 非常感谢您的解释和快速回复!它现在运行良好!
猜你喜欢
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
相关资源
最近更新 更多