前提・実現したいこと
Ruby on Railsでform_htmlで保存した値をcontrollerで計算して、show_htmlに表示したいです。
ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
controllerで、計算した値がDBに保存されていない。
エラーメッセージ
Processing by HethsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Bk4J62dGwe+i53+NSFGXqdFeXxEIgjymo2H0EUguEKd2FnJX8A5GdHrDd0vZqBGXCx9O57OZ46pr1L9HjtySqQ==", "heth"=>{"name"=>"いけだ", "age"=>"24", "gender"=>"male", "weight"=>"68", "revel"=>"number_two", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}} (0.6ms) begin transaction ↳ app/controllers/heths_controller.rb:140 Heth Create (16.5ms) INSERT INTO "heths" ("name", "age", "gender", "weight", "created_at", "updated_at", "revel", "password_digest") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["name", "いけだ"], ["age", 24], ["gender", "male"], ["weight", 68.0], ["created_at", "2021-02-04 09:29:40.509335"], ["updated_at", "2021-02-04 09:29:40.509335"], ["revel", "number_two"], ["password_digest", "$2a$12$/NHI5RxxLtZRrLslaFN8bOmdw4bgpBUSrrvF.or4ffqSCrn2Gm3Uy"]] ↳ app/controllers/heths_controller.rb:140 (10.5ms) commit transaction ↳ app/controllers/heths_controller.rb:140 Redirected to http://192.168.33.10:3000/heths/71 Completed 302 Found in 487ms (ActiveRecord: 27.6ms) Started GET "/heths/71" for 192.168.33.1 at 2021-02-05 08:56:43 +0000 Cannot render console from 192.168.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by HethsController#show as HTML Parameters: {"id"=>"71"} Heth Load (4.6ms) SELECT "heths".* FROM "heths" WHERE "heths"."id" = ? LIMIT ? [["id", 71], ["LIMIT", 1]] ↳ app/controllers/heths_controller.rb:182 CACHE Heth Load (0.1ms) SELECT "heths".* FROM "heths" WHERE "heths"."id" = ? LIMIT ? [["id", 71], ["LIMIT", 1]] ↳ app/controllers/heths_controller.rb:18 Rendering heths/show.html.erb within layouts/application Rendered heths/show.html.erb within layouts/application (1.6ms) Completed 200 OK in 346ms (Views: 265.5ms | ActiveRecord: 4.6ms)
該当のソースコード
**form_html** <%= form_with(model: heth, local: true) do |form| %> <% if heth.errors.any? %> <div class="er"> <div id="error_explanation"> <h2><%= pluralize(heth.errors.count, "error") %> が発生しました。</h2> <ul> <% heth.errors.full_messages.each do |message| %> <ul><%= message %></ul> <% end %> </ul> </div> </div> <% end %> <div class="form"> <h1>アカウント登録</h1> <div class="field"> <%= form.label :name, "名前" %> <%= form.text_field :name %> </div> <div class="field"> <%= form.label :age, "年齢" %> <%= form.number_field :age %> </div> <div class="field"> <%= form.radio_button :gender, :male %> <%= form.label :gender, "男性", {value: :"男性", style: "display: inline-block;"} %> <%= form.radio_button :gender, :female %> <%= form.label :gender, "女性", {value: :"女性", style: "display: inline-block;"} %> </div> <div class="field"> <%= form.label :weight, "体重" %> <%= form.number_field :weight %> </div> <div class="field"> <%= form.radio_button :revel, :number_one %> <%= form.label :revel, "レベルⅠ", {value: :"レベルⅠ", style: "display: inline-block;"} %> <%= form.radio_button :revel, :number_two %> <%= form.label :revel, "レベルⅡ", {value: :"レベルⅡ", style: "display: inline-block;"} %> <%= form.radio_button :revel, :number_three %> <%= form.label :revel, "レベルⅢ", {value: :"レベルⅢ", style: "display: inline-block;"} %> </div> <div class="text"> <h3>日頃の生活の運動レベルを選んで下さい</h3> <h3>レベルⅠ:生活の大部分が座位で、静的な活動が中心の場合</h3> <h3>レベルⅡ:座位中心の仕事だが、職場内での移動や立位での作業・接客等、あるいは通勤・買物・家事、軽いスポーツ等のいずれかを含む場合</h3> <h3>レベルⅢ:移動や立位の多い仕事への従事者。あるいは、スポーツなど余暇における活発な運動習慣をもっている場合</h3> </div> <div class="field"> <%= form.label :password, "パスワード" %> <%= form.password_field :password %> </div> <div class="field"> <%= form.label :password_confirmation, "もう一度パスワードを入力して下さい" %> <%= form.password_field :password_confirmation %> </div> <div class="bota"> <input type="submit" value"登録" class="button"> <div class="back"> <span><%= link_to 'Back', heths_path %></span> </div> </div> </div> <% end %> </body> **heths_controller** def index @heths = Heth.all end # GET /heths/1 # GET /heths/1.json # 個別のデータを扱う def show # Hethモデルを使って、Heathのテーブルから id が params[:id]のデータを取得する # /healsths?id=1 # @heth = Heth.find_by(id: 1) @heth = Heth.find_by(id : params[id]) age = params[:age] gender = params[:gender] weight = params[:weight] if gender == "male" if age == 1 || age == 2 a = 61 elsif age >= 3 && age <= 5 a = 54.8 elsif age == 6 || age == 7 a = 44.3 elsif age == 8 || age == 9 a = 40.8 elsif age == 10 || age == 11 a = 37.4 elsif age >= 12 && age <= 14 a = 31 elsif age>= 15 && age <= 17 a = 27 elsif age >= 18 && age <= 29 a = 24 elsif age >= 30 && age <= 49 a = 22.3 elsif age >= 50 a = 21.5 end elsif gender == "female" if age == 1 || age == 2 a = 59.7 elsif age >= 3 && age <= 5 a = 52.2 elsif age == 6 || age == 7 a = 41.9 elsif age == 8 || age == 9 a = 38.3 elsif age == 10 || age == 11 a = 34.8 elsif age >= 12 && age <= 14 a = 29.6 elsif age >= 15 && age <= 17 a = 25.3 elsif age >= 18 && age <= 29 a = 22.1 elsif age >= 30 && age <= 49 a = 21.7 elsif age >= 50 a = 20.7 end vasekcal = a * weight end level = params[:level] if level == "number_one" if age == 1 || age == 2 rv = 1 elsif age >= 3 && age <= 5 rv = 1 elsif age == 6 || age == 7 rv = 1.35 elsif age == 8 || age == 9 rv = 1.4 elsif age >= 10 && age <= 14 rv = 1.45 elsif age >= 15 && age <= 17 rv = 1.55 elsif age >= 18 && age <= 69 rv = 1.5 elsif age >= 70 rv = 1.45 end elsif level == "number_two" if age == 1 || age == 2 rv = 1.35 elsif age >= 3 && age <= 5 rv = 1.45 elsif age == 6 || age == 7 rv = 1.55 elsif age == 8 || age == 9 rv = 1.6 elsif age >= 10 && age <= 14 rv = 1.65 elsif age >= 15 && age <= 69 rv = 1.75 elsif age >= 70 rv = 1.7 end elsif level == "number_three" if age == 1 || age == 2 rv = 1 elsif age >= 3 && age <= 5 rv = 1 elsif age == 6 || age == 7 rv = 1.75 elsif age == 8 || age == 9 rv = 1.8 elsif age >= 10 && age <= 14 rv = 1.85 elsif age >= 15 && age <= 17 rv = 1.95 elsif age >= 18 && age <= 69 rv = 2 elsif age >= 70 rv = 1.95 end kcal = vasekcal * rv end @heth_kcal = Heth.new(kcal) end # GET /heths/new def new @heth = Heth.new(user_params) end # GET /heths/1/edit def edit end # POST /heths # POST /heths.json def create @heth = Heth.new(heth_params) @heth_kcal = kcal @heth_kcal.save respond_to do |format| if @heth.save format.html { redirect_to @heth, notice: 'Heth was successfully created.' } format.json { render :show, status: :created, location: @heth } else format.html { render :new } format.json { render json: @heth.errors, status: :unprocessable_entity } end end end private # Use callbacks to share common setup or constraints between actions. def set_heth @heth = Heth.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. # permit(:カラム名) def heth_params params.require(:heth).permit(:name, :age, :gender, :weight, :revel, :kcal, :password, :password_confirmation) end **show_html** <!DOCTYPE html> <html lang="ja"> <head> </head> <body> <div class="daycal"> <h1>1日の摂取カロリー目安</h1> <h1><%= @heth_kcal %></h1> <div class="lin"> <%= link_to 'Edit', edit_heth_path(@heths) %> <%= link_to '今日の摂取カロリー', new_health_path %> </div> </div> </body> <footer> </footer> </html> コード
ソースコード
試したこと
controllerに値を保存するために、def create で.saveを追加しました。
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/07 11:07
2021/02/07 11:13
2021/02/09 09:26
2021/02/09 09:29
2021/02/09 09:30
2021/02/09 09:31