Max Vinicius
Max Vinicius

Reputation: 647

How can I retrieve parameters after validation fails?

After the validation fails i lose the user_id parameter from link_to, causing the error below in the method:

undefined method `bank_account' for nil:NilClass

My models:

#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"

My link_to and form input:

#link_to
<%= link_to ("Transfer credits to "[email protected]), new_account_transaction_path(user_id: params[:id]) %>

#form input
<%= f.input :target_bank_id, as: :hidden, input_html: { value: @user.bank_account.id } %>

My controller:

  #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

Log:

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'

Upvotes: 0

Views: 496

Answers (1)

arieljuod
arieljuod

Reputation: 15838

I would change your form to this:

<%= simple_form_for @account_transaction do |f| %>
  ....

  <%= hidden_field_tag :user_id, @user.id %>

  <div class='form-inputs'>
    <%= f.input :amount %>
  </div>

  ....
<% end %>

Note that you are not exposing bank account ids, which is safer. You have user_id as a parameter so you can get it's account and the current_user to get he's.

Now on your create action

def create
  @user = User.find(params[:user_id])
  @account_transaction = AccountTransaction.new(
    source_bank_account: current_user.bank_account,
    target_bank_account: @user.bank_account,
    amount: params[:account_transaction][:amount]
  )

  if @account_transaction.save
    redirect_to @account_transaction, notice: "Transfer Completed."
  else
    render :new
  end
end

If validations fail, then you already have @user variable to fill in the form.

Upvotes: 1

Related Questions