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

undefined method `new' for nil:NilClass

这个问题的名字已经被使用了,但是这个问题是完全不同的。在 localhost:3000/forums/new 中,它表示:

nil:NilClass

的未定义方法“新”

我的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(在视图>论坛下):

 <h1>New Forum</h1>

 = render 'forum'

 = link_to "Back", root_path

我的 index.html.erb 在 vi​​ew>forums 下完全是空的。 我的应用程序控制器:

 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

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