Help us understand the problem. What is going on with this article?

number_fieldの値をそのまま計算しようとしたら怒られた

More than 3 years have passed since last update.

金額を入力するfieldを作成

***.haml
= form_tag update_path  do
  = label_tag :price
  = number_field_tag :price

  .actions
    = submit_tag 'Save'
    = link_to 'Back', :back

入力された金額を100倍して保存

Controller.rb
  def update
    if params[:price] != nil
      item.price = params[:price] * 100
      item.save
    end
  end

金額を入力してSaveをするとString can't be coerced into BigDecimalと怒られた。

改めてparamsの中を確認して見ると"price"=>"100"となっていた
number_fieldでもparamsで取得したさいには、文字型なんですね。。。

というわけで、コントローラーを修正

Controller.rb
  def update
    if params[:price] != nil
      item.price = params[:price].to_i * 100
      item.save
    end
  end

以上

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away