表单提交的值为nil(缺少param或值为空:amount)

时间:2017-04-30 03:48:51

标签: ruby-on-rails ruby null routes

我是铁杆的新手,请原谅我。我知道我收到此错误,因为我指定了参数所需的数量。如果我没有,我最终会得到一个只有一个transaction_id的事务条目,其他每个字段都是nil。我相信我的问题必须处理我的路线,但我不确定什么是错的。

我的错误:

  在TransactionController #create中创建ActionController :: ParameterMissing

     

param丢失或值为空:金额

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

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

我的TransactionsController:

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

如果您对此错误有所了解,我将不胜感激。

1 个答案:

答案 0 :(得分: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"