Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert the value from a form field into the model? #707

Open
bvirlet opened this issue Oct 17, 2024 · 1 comment
Open

How to insert the value from a form field into the model? #707

bvirlet opened this issue Oct 17, 2024 · 1 comment

Comments

@bvirlet
Copy link

bvirlet commented Oct 17, 2024

Hi folks,

Thanks for this great library.

I'm currently running in an error. When using the following form:

  <div class="field form-group">
    <%= form.label :amount, 'Amount %>
    <%= form.number_field :amount, step: :any, class: 'form-control' %>
  </div>

Controller:

  def create
    @convertible_note = @company.convertible_notes.build(convertible_note_params)
    if @convertible_note.save
      redirect_to portfolio_company_path(@portfolio, @company), notice: 'Convertible Note was successfully created.'
    else
      render :new, status: :unprocessable_entity
    end
  end

…

  def convertible_note_params
    params.require(:convertible_note).permit(:amount, :amount_cents)
  end

Model:

  monetize :amount_cents, with_model_currency: :currency

I'm getting the following error on creation:
Amount cents is not a number

Am I doing something wrong? What pattern do you recommend to properly fix this?

Thanks!

@bvirlet
Copy link
Author

bvirlet commented Oct 17, 2024

I have found one way of addressing this like so:

  def convertible_note_params
    params.require(:convertible_note).permit(:amount)
          .tap do |whitelisted|
      whitelisted[:amount_cents] = whitelisted.delete(:amount).to_f * 100 if whitelisted[:amount]
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant