LoginSignup
0
1

More than 3 years have passed since last update.

【NoMethodError】newアクションで発生した際の解決法

Last updated at Posted at 2020-05-29

概要

登録画面に遷移したいのに、エラーになって全然表示できない:sob:
状況に陥り、何時間も悪戦苦闘した結果、解決しましたので備忘録として残します。

事象

下記のようなエラーが発生:zap:
スクリーンショット 2020-05-29 11.45.21.png

routes.rb
Rails.application.routes.draw do
  devise_for :users
  root "groups#index"

  resources :users, only: [:edit, :update]

  resources :groups, only: [:index, :new, :create, :edit, :update] do
    resources :tasks, only: [:index, :new, :create, :edit, :update, :destroy]
  end
end
controller.html.haml
  def new
    @task = Task.new
  end
main_view.html.haml
#省略
.new_display
  = form_for @task do |f|
#省略

エラーを見た感想

おかしなとこない:disappointed_relieved:
何言ってんねん:persevere:
newアクションだからデータないやんかーーー
と一人で悶々としてました。。。

原因

原因は【form_forが自動的に生成してくれるパスは複数形のみ】ということ!!
エラー文にも記載ありました!

undefind method 'tasks_path' for ~

私は、その箇所を読んでも何言ってんの??そんなパスないよ:triumph:
と思ってましたが:sweat:

解決策

viewを下記のように変更しました!

main_view.html.haml
#省略
.new_display
  = form_for @task, url: group_tasks_path do |f|
#省略

画面

スクリーンショット 2020-05-29 13.40.42.png

似たようなことでお悩みの方の足掛かりになれば幸いです:relaxed:

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1