Ruby on rails 简单表单未定义方法模型名称3类错误
所以我只是嵌套了一些以前没有嵌套过的资源,因为我一直在尝试修复所有的路径引用。我遇到的最大问题是,在一个较大的嵌套资源中有两个嵌套资源,如下所示: 用户->照片->评论 在我的表单上,它不断给我以下错误 /users/2/photos/2/comments/new:String的未定义方法“model_name” 错误页面显示源代码位于以下my comments/\u form partial的第1行附近:Ruby on rails 简单表单未定义方法模型名称3类错误,ruby-on-rails,ruby,routes,nested-resources,Ruby On Rails,Ruby,Routes,Nested Resources,所以我只是嵌套了一些以前没有嵌套过的资源,因为我一直在尝试修复所有的路径引用。我遇到的最大问题是,在一个较大的嵌套资源中有两个嵌套资源,如下所示: 用户->照片->评论 在我的表单上,它不断给我以下错误 /users/2/photos/2/comments/new:String的未定义方法“model_name” 错误页面显示源代码位于以下my comments/\u form partial的第1行附近: = simple_form_for ([@comment, new_user_photo
= simple_form_for ([@comment, new_user_photo_comment_path(@user,@photos,@comment)]) do |f|
= f.input :content, label: "Reply to thread"
=f.button :submit, class: "button"
这是我的评论:
class CommentsController < ApplicationController
before_action :authenticate_user!
def new
@photo=Photo.find(params[:photo_id])
end
def create
@photo =Photo.find(params[:photo_id])
@comment=Comment.create(params[:comment].permit(:content, :id, :photo_id))
@comment.user_id= current_user.id
@comment.photo_id= @photo.id
@user= User.find_by(params[:id])
if @comment.save
redirect_to user_photo_path(@photo, @user)
else
render 'comments/new'
end
end
end
首先,最好不要将资源嵌套的深度超过两倍。 您应该考虑仅在照片内嵌套注释。在routes中也可以这样做。rb:
resources :users do
resources :photos
end
resources :photos do
resources :comments
end
你的错误是因为
=用于[@comment,new\u user\u photo\u comment的简单表单_path@user,@photos,@comment]do | f|
给出as参数的简单形式方法:
1-模型注释
2-字符串/用户/2/照片/2/评论/新建
要设置正确的路径形式动作,表单生成器需要模型作为所有参数。
也许像
=简单表格(适用于[@user、@photos、@comment]do | f|
应该有用