1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】NoMethodError in XXX#new undefined method `xxx_path' のエラーについて

Last updated at Posted at 2023-05-11

記事1を参考にRailsでお仕事辞退機能(辞退理由を送信する)を実装中に以下のエラーに遭遇しました。

NoMethodError in Cancels#new
Showing /Users/me/environment/アプリ名/app/views/cancels/new.html.erb where line #1 raised:

undefined method `cancels_path' for #<ActionView::Base:0x0000000001d308>

              recipient.public_send(method, *args, options)
                       ^^^^^^^^^^^^

image.png

以下がコードになります。

config/routes.rb
Rails.application.routes.draw do
  resources :jobs, only: [:new, :create, :edit, :index, :show, :destroy] do
    resource :applicants, only: [:create, :destroy]
    resource :cancels, only: [:create, :new, :destroy]
  end
  root "home#top"
  resources :users, only: [:new, :create, :edit, :destroy, :update]
  # post "users/create" => "users#create", as: "create_users"
  # get "users/:id/edit" => "users#edit", as:"edit_users"
  # post "users/:id/update" => "users#update", as:"update_users"
  get "signup", to: "users#new", as: "new_users"
  resources :guest_sessions, only: [:create]
  # post "guest_login", to: "guest_sessions#guest_login"
  # get "guest_login", to: "guest_sessions#guest_login"
  post "logout" => "sessions#destroy"
  # get "logout" => "sessions#destroy"
  get "login", to: "sessions#new", as: "new_sessions"
  post  "login", to: "sessions#create", as: "create_sessions"
  delete "logout", to: "sessions#destroy", as: "destroy_sessions"
  # get "users/:id/destroy", to: "users#destroy"
  # delete "users/:id/destroy", to: "users#destroy", as: "destroy_users"
end
$ rails routes
                                  Prefix Verb   URI Pattern                                                                                       Controller#Action
                          job_applicants DELETE /jobs/:job_id/applicants(.:format)                                                                applicants#destroy
                                         POST   /jobs/:job_id/applicants(.:format)                                                                applicants#create
                         new_job_cancels GET    /jobs/:job_id/cancels/new(.:format)                                                               cancels#new
                             job_cancels DELETE /jobs/:job_id/cancels(.:format)                                                                   cancels#destroy
                                         POST   /jobs/:job_id/cancels(.:format)                                                                   cancels#create
                                    jobs GET    /jobs(.:format)                                                                                   jobs#index
                                         POST   /jobs(.:format)                                                                                   jobs#create
                                 new_job GET    /jobs/new(.:format)                                                                               jobs#new
                                edit_job GET    /jobs/:id/edit(.:format)                                                                          jobs#edit
                                     job GET    /jobs/:id(.:format)                                                                               jobs#show
                                         DELETE /jobs/:id(.:format)                                                                               jobs#destroy
                                    root GET    /                                                                                                 home#top
                                   users POST   /users(.:format)                                                                                  users#create
                                new_user GET    /users/new(.:format)                                                                              users#new
                               edit_user GET    /users/:id/edit(.:format)                                                                         users#edit
                                    user PATCH  /users/:id(.:format)                                                                              users#update
                                         PUT    /users/:id(.:format)                                                                              users#update
                                         DELETE /users/:id(.:format)                                                                              users#destroy
                               new_users GET    /signup(.:format)                                                                                 users#new
                          guest_sessions POST   /guest_sessions(.:format)                                                                         guest_sessions#create
                                  logout POST   /logout(.:format)                                                                                 sessions#destroy
                            new_sessions GET    /login(.:format)                                                                                  sessions#new
                         create_sessions POST   /login(.:format)                                                                                  sessions#create
                        destroy_sessions DELETE /logout(.:format)                                                                                 sessions#destroy
           rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                           action_mailbox/ingresses/postmark/inbound_emails#create
              rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                              action_mailbox/ingresses/relay/inbound_emails#create
           rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                           action_mailbox/ingresses/sendgrid/inbound_emails#create
     rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#health_check
           rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#create
            rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                                       action_mailbox/ingresses/mailgun/inbound_emails#create
          rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#index
                                         POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#create
       new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                                      rails/conductor/action_mailbox/inbound_emails#new
      edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                                 rails/conductor/action_mailbox/inbound_emails#edit
           rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#show
                                         PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#update
                                         PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#update
                                         DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET    /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format)                              rails/conductor/action_mailbox/inbound_emails/sources#new
   rails_conductor_inbound_email_sources POST   /rails/conductor/action_mailbox/inbound_emails/sources(.:format)                                  rails/conductor/action_mailbox/inbound_emails/sources#create
   rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                               rails/conductor/action_mailbox/reroutes#create
rails_conductor_inbound_email_incinerate POST   /rails/conductor/action_mailbox/:inbound_email_id/incinerate(.:format)                            rails/conductor/action_mailbox/incinerates#create
                      rails_service_blob GET    /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format)                               active_storage/blobs/redirect#show
                rails_service_blob_proxy GET    /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format)                                  active_storage/blobs/proxy#show
                                         GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                                        active_storage/blobs/redirect#show
               rails_blob_representation GET    /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
         rails_blob_representation_proxy GET    /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)    active_storage/representations/proxy#show
                                         GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)          active_storage/representations/redirect#show
                      rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                                       active_storage/disk#show
               update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                               active_storage/disk#update
                    rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                                    active_storage/direct_uploads#create
app/controllers/cancels_controller.rb
class CancelsController < ApplicationController

  def show
    @job = Job.find_by(id: params[:id])
  end

  def new
    @cancel = Cancel.new
  end

  def create
    Cancel.create(cancel_params)
    redirect_to root_path
  end

  private
  def cancel_params
    params.require(:cancel).permit(:reason).merge(user_id: @current_user.id)
  end

end
app/views/cancels/new.html.erb
<%= form_with(model: @cancel) do |f| %>
  <%= f.text_area(:reason, placeholder: "辞退理由を入力") %>
  <%= f.submit("辞退理由を送信") %>
<% end %>

記事2によると、

調べると、以上の「= form_with model: @review, local: true do |f|」のコードにおいては、form_with model: @review の箇所が内部的にpolymorphic_path(@review)というメソッドを実行しているそうで、 その実行結果がreviews_pathとなるのでそこでエラーが起きているらしい。

ということで、パスが意図せず作られてしまう(?)ことで発生するエラーのようです。

記事3では、アソシエーションの設定が上手くできていない可能性があると書かれており、チェックしてみましたが特におかしい点は見当たりませんでした。

記事2ではform_forで解決していますが、form_forは現在では非推奨なので、以下のようにしたところ一応エラーは出なくなりました。

app/views/cancels/new.html.erb
<%= form_with(url: job_cancels_path) do |f| %>
  <%= f.text_area(:reason, placeholder: "辞退理由を入力") %>
  <%= f.submit("辞退理由を送信") %>
<% end %>

image.png

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?