gpt4 book ai didi

ruby-on-rails - 未定义方法 `new' 为 nil :NilClass

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:33 24 4
gpt4 key购买 nike

这个问题的名称已被使用,但这个问题是完全不同的。在 localhost:3000/forums/new 中,它是这样说的:

nil:NilClass 的未定义方法“new”

我的 forums_controller.rb:

 class ForumsController < ApplicationController
def index
end

def forum
end

def new
@forum = forum.new
end

def create
@forum = forum.new (forum_params)
end

private

def forum_params
params.require(:forum).permit(:title, :text)
end
end

我的 new.html.erb(在 View >论坛下):

 <h1>New Forum</h1>

= render 'forum'

= link_to "Back", root_path

我在view下的index.html.erb>论坛完全是空的。我的应用程序 Controller :

 class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end

_forum.html.erb:

 = simple_form_for @forum, html: { multipart: true } do |f|
- if @forum.errors.any?
#errors
%h2
= pluralize(@forum.errors.count, "error")
perevented this Forum for saving
%ul
- @forum.errors.full_messages.each do |msg|
%li= msg

.forum-group
= f.input :title, input_html { class: 'forum-control' }

.forum-group
= f.input :text, input_html { class: 'forum-control' }

= f.button :submit,class: "btn btn-primary"

有人知道怎么解决吗?如果有任何代码遗漏,请评论,我会添加它。

最佳答案

我相信你想写这样的东西

Forum.new

不是

forum.new

forum 显然是 nil 就您将 forum 定义为

def forum
end

我希望您已经在您的应用程序中定义了论坛模型

关于ruby-on-rails - 未定义方法 `new' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33965530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com