繁体   English   中英

simple_form未定义方法model_name 3类错误

[英]simple_form undefined method model_name 3 classes error

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

用户 - > Photos->评论

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

“ / users / 2 / photos / 2 / comments / new”的未定义方法“ model_name”:字符串

错误页面显示源位于以下代码的第1行(我的注释/ _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"

这是我的Comments控制器:

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| 应该管用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM