質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

1494閲覧

rails updateしようとするも、Routing Errorとなってしまう。

hurousyotoku500

総合スコア27

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/11/09 06:23

編集2020/11/10 11:17

データベースのupdateの処理を書いていますが、putしようとしているのですが、なぜかNo route matches [POST]と出てしまいます。
やろうとしていることは、updateなのに、なぜPOSTについて言われているのかわかりません。
宜しくお願い致します。
イメージ説明
ルーティング
イメージ説明

rb

1#rotues.rb 2 get "/archives",to: 'archives#index' ,as: "archives_index" 3 get "archives/show/:round_id",to: 'archives#show' ,as: 'score_card' 4 get "archives/edit/:round_id",to: 'archives#edit' ,as: 'scorecard_edit' 5 get "archives/edit/:round_id/:hole_number",to: 'archives#edit_score' ,as: 'edit_hole_score' 6 get "archives/edit_score/:round_id/:hole_number",to: 'archives#hole_score_edit' ,as: 'score_update' 7 post "archives/edit/:round_id/:hole_number",to: 'archives#update' ,as: 'hole_score_update'

rb

1#archives_controller.rb 2class ArchivesController < ApplicationController 3 def index 4 @scores = Score.all 5 @score_round = Score.distinct.pluck(:course, :user_id, :round_id) 6 7 end 8 def show 9 # @score_card = Score. 10 @coursename = params[:round_id] 11 @score_card_score = Score.where(round_id: params[:round_id]) #ホールごとのスコアを取得 12 @score_card_course = Score.where(round_id: params[:round_id]).first #ラウンドしたコース、日付を取得 13 @score_sum = Score.where(round_id: params[:round_id]).sum(:hole_score) 14 end 15 def edit 16 # @score_card = Score. 17 @coursename = params[:round_id] 18 @score_card_score = Score.where(round_id: params[:round_id]) #ホールごとのスコアを取得 19 @score_card_course = Score.where(round_id: params[:round_id]).first #ラウンドしたコース、日付を取得 20 @score_sum = Score.where(round_id: params[:round_id]).sum(:hole_score) 21 end 22 def edit_score 23 @hole_number = params[:hole_number] 24 @round_id = params[:round_id] 25 @score = Score.find_by(round_id: params[:round_id], hole_number: params[:hole_number]) 26 end 27 def hole_score_edit 28 @score = Score.find_by(round_id: params[:round_id], hole_number: params[:hole_number]) 29 end 30 def create 31 end 32 def update 33 @score = Score.find_by(round_id: params[:round_id], hole_number: params[:hole_number]) 34 @score.update(hole_score: @score) 35 redirect_to score_card_path 36 end 37end 38

rb

1#edit_score.html.erb 2<%= form_with url:hole_score_update_path do |f| %> 3 <%= f.text_field :hole_score %> 4 <%= f.submit "編集する" %> 5<% end %> 6

rb

1#edit.html.erb 2<h1>スコアカード</h1> 3 4<p>ラウンドID<%= @coursename %></p> 5<p> <%= @score_card_score %></p> 6<p>プレイしたコース: <%= @score_card_course.course %></p> 7<p>プレイヤー名: <%= @score_card_course.user.name %></p> 8<p>プレイ日時: <%= @score_card_course.created_at.strftime('%Y/%m/%d') %></p> 9<p>トータルスコア: <%= format('%+d', @score_sum + 72) %></p> 10 11<% @score_card_score.each do |m| %> 12<span> <%= m.hole_number %>H: </span> 13<span> <%= format('%+d',m.hole_score) %> </span> 14<span> <%= link_to "編集", edit_hole_score_path(round_id: m.round_id, hole_number: m.hole_number) %> </span><br> 15<% end %> 16 17

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2020/11/09 10:12

edit_score.html.erb からのsubmitですか?
hurousyotoku500

2020/11/09 11:24

そうです、edit_score.html.erb からのsubmitです。
guest

回答2

0

自己解決

自己解決しました。
def updateで、
@score.update(hole_score: @score)
ではなくて、
@score.update(hole_score: params[:hole_score])
でした。

投稿2020/11/11 12:48

hurousyotoku500

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

def edit_score では @score = Score.find_by ですが edit_score.html.erb では form_with(model: @score_edit です。
@score_edit が nill なのが原因でしょう。

これを @score に直すとそのエラーは出ませんが ScoresController のupdateに行きます。

投稿2020/11/09 11:51

winterboum

総合スコア23333

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hurousyotoku500

2020/11/09 13:02

ご回答ありがとうございます。 @score_editを@scoreに変更してみましたが、 「NoMethodError in Archives#edit_score」 「undefined method `score_path' for #<#<Class:0x00007f98b1223e08>:0x00007f98b149fbc8> Did you mean? score_update_path」 と言われてしまいました。
winterboum

2020/11/09 13:48

どのcontrollerのどのactionで処理したいのでしょう。 そのrouteの定義と、そのpathになるようにform_withの編集を
hurousyotoku500

2020/11/10 11:03

コード修正してみましたが、やはり、NoMethodError in Archives#edit_scoreが出てしまいます。 @score.update(hole_score: params[:hole_score])の書き方がいまいちよくわからないです。
hurousyotoku500

2020/11/10 11:19

再度修正しました。なにかしら、値の更新はしてそうなのですが、値がNULLになってしまいます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問