После сбоя проверки я теряю параметр user_id из link_to, что приводит к ошибке ниже в методе:
undefined method `bank_account' for nil:NilClass
Мои модели:
#user.rb
has_one :bank_account, inverse_of: :user, dependent: :destroy
#bank_account.rb
belongs_to :user, inverse_of: :bank_account
has_many :incoming_transfers, foreign_key: "target_bank_id", class_name: "AccountTransaction"
has_many :made_transfers, foreign_key: "source_bank_id", class_name: "AccountTransaction"
# account_transaction.rb
belongs_to :target_bank_account, foreign_key: "target_bank_id", class_name: "BankAccount"
belongs_to :source_bank_account, foreign_key: "source_bank_id", class_name: "BankAccount"
Мой link_to и ввод формы:
#link_to
<%= link_to ("Transfer credits to "+@user.name), new_account_transaction_path(user_id: params[:id]) %>
#form input
<%= f.input :target_bank_id, as: :hidden, input_html: { value: @user.bank_account.id } %>
Мой контроллер:
#account_transactions_controller.rb
def new
@user = User.friendly.find(params[:user_id])
@account_transaction = AccountTransaction.new
end
def create
@account_transaction = AccountTransaction.new(account_transaction_params)
if @account_transaction.save
redirect_to @account_transaction, notice: "Transfer Completed."
else
render :new
end
end
Журнал:
NoMethodError - undefined method `bank_account' for nil:NilClass:
app/views/account_transactions/_form.html.erb:7:in `block in _app_views_account_transactions__form_html_erb___1756003144041352821_70262592920360'
app/views/account_transactions/_form.html.erb:1:in `_app_views_account_transactions__form_html_erb___1756003144041352821_70262592920360'
app/views/account_transactions/new.html.erb:3:in `_app_views_account_transactions_new_html_erb__1529716313223870974_70262647907340'
app/controllers/account_transactions_controller.rb:33:in `create'