Ruby on rails Rails-未定义的方法错误
我有以下型号: 列表中有很多课程,其中有很多课程项目 在一部分中,我试图创建一个表单来创建课程项目 在rake routes中,我尝试使用的路由被列为“/listings/:listing_id/courses/:id/course_item(:format)” 然而,在试图张贴到这个路线,我得到以下错误。任何帮助都将不胜感激 错误Ruby on rails Rails-未定义的方法错误,ruby-on-rails,nomethoderror,Ruby On Rails,Nomethoderror,我有以下型号: 列表中有很多课程,其中有很多课程项目 在一部分中,我试图创建一个表单来创建课程项目 在rake routes中,我尝试使用的路由被列为“/listings/:listing_id/courses/:id/course_item(:format)” 然而,在试图张贴到这个路线,我得到以下错误。任何帮助都将不胜感激 错误 NoMethodError in Courses#show Showing /Users/Jack_R/code/rails/planet_study/app/vi
NoMethodError in Courses#show
Showing /Users/Jack_R/code/rails/planet_study/app/views/course_item/_new.html.erb where line #3 raised:
undefined method `listing_course_items_path' for #<#<Class:0x007ff8616081f8>:0x007ff866d3ba88>
型号
resources :listings, except: [:edit] do
member do
...
end
resources :listing_photos, only: [:create, :destroy]
resources :courses, except: [:edit] do
#delete :destroy, on: :collection
member do
...
resources :course_item do
resources :reservations, only: [:create, :new, :index]
end
end
end
class Listing < ApplicationRecord
belongs_to :user
has_many :courses
has_many :listing_photos
end
class Course < ApplicationRecord
belongs_to :listing, optional: true
has_many :course_items
end
class CourseItem < ApplicationRecord
belongs_to :course
has_many :reservations
end
类列表
您应该使用rake routes
然后查看确切的路径名这是确切的路径名:“POST/listings/:listing\u id/courses/:id/courseu item(:format)courseu item\u item#create”问题是我不明白为什么这不起作用请为您的模型添加代码。@Ankit在上面添加,谢谢,甚至谢谢。我尝试了第一个建议,但还是犯了同样的错误。对于第二个参数,我得到了“表单中的第一个参数不能包含零或为空”@Jack Riminton,你也应该在课程项目#new
中更改@listings=Listing.find(params[:Listing\u id])
到@Listing=Listing.find(params[:Listing\u id])
:)我在路由中也有一个错误。使用上述所有建议,新的错误是:“未定义的方法`listing_course_course_items_path',如果我将表单更改为“([@listing,@course,@course_item]),则错误再次出现:“表单中的第一个参数不能包含nil或为空”
resources :listings, except: [:edit] do
member do
...
end
resources :listing_photos, only: [:create, :destroy]
resources :courses, except: [:edit] do
#delete :destroy, on: :collection
member do
...
resources :course_item do
resources :reservations, only: [:create, :new, :index]
end
end
end
class Listing < ApplicationRecord
belongs_to :user
has_many :courses
has_many :listing_photos
end
class Course < ApplicationRecord
belongs_to :listing, optional: true
has_many :course_items
end
class CourseItem < ApplicationRecord
belongs_to :course
has_many :reservations
end