###前提・実現したいこと
ruby on railsで、入力フォームで入力した値を記録する機能をつくっています。
入力フォーム(form_for)にて値(amount)を入力してsubmitボタンを押すと、
データベースにレコードは作成されるのですが、
入力した値は記録されず、nullになっていまい困っています。
どうすれば、入力した値を記録をすることができるようになるのか
教えて頂ければ幸いです。
よろしくお願いします。
###現状のコードです
記録をするテーブルの内容です。
create_table "account_transactions", force: :cascade do |t| t.decimal "amount" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "withdrawal_account_id" t.integer "deposit_account_id" t.index ["deposit_account_id"], name: "index_account_transactions_on_deposit_account_id" t.index ["withdrawal_account_id"], name: "index_account_transactions_on_withdrawal_account_id" end
app/views/account_transactions/new.html.erb(入力フォームの内容です。)
ruby
1<%= form_for @account_transaction do |f|%> 2<div class="field"> 3 <%= f.label :送金額 %> 4 <%= f.number_field :amount %> 5 <%= f.submit"記録する" %> 6</div> 7<% end %>
/home/vagrant/megterra/app/controllers/account_transactions_controller.rb
(コントローラの内容です。)
※他にもwithdrawal_account_idとdeposit_account_idという入力項目があるのですが、
説明が長くなるのでコントローラに直接記入しています。
ruby
1class AccountTransactionsController < ApplicationController 2 def create 3 @account_transaction = AccountTransaction.new( 4 withdrawal_account_id: 20,#質問用の為簡略化しています 5 deposit_account_id: 18,#質問用の為簡略化しています 6 amount: params[:amount] 7 ) 8 if @account_transaction.save 9 redirect_to root_path 10 else 11 render "new" 12 end 13 end 14 15 def account_transaction_params 16 params.require(:account_transaction).permit(:amount) 17 end 18end
###補足情報(言語/FW/ツール等のバージョンなど)
Rails 5.1.3
ruby 2.4.1
devise (4.3.0)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/22 02:21