simple_form 未定义方法 model_name 3 类 错误

simple_form undefined method model_name 3 classes error

所以我只是嵌套了一些之前没有嵌套的资源,因为我一直在尝试修复所有路径引用。我遇到的最大问题是在一个较大的嵌套资源中有 2 个嵌套资源,如下所示:

Users->Photos->Comments

在我的表单上,它一直给我以下错误

undefined method `model_name' for "/users/2/photos/2/comments/new":String

错误页面显示源代码位于以下第 1 行附近(我的 comments/_form 部分):

= 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

而你的错误是因为 = simple_form_for ([@comment, new_user_photo_comment_path(@user,@photos,@comment)]) do |f| 给出方法 simple_form_for 作为参数:
1 - 模型评论
2 - 字符串 /users/2/photos/2/comments/new

设置正确的路径(表单操作)表单构建器需要模型作为所有参数。 也许像 = simple_form_for ([@user,@photos,@comment]) do |f| 应该工作