Yahs Hef
Yahs Hef

Reputation: 797

undefined method `new' for nil:NilClass

This question's name has been used but this question is completely different. In localhost:3000/forums/new, it says this:

undefined method `new' for nil:NilClass

my 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

my new.html.erb (under view>forums):

 <h1>New Forum</h1>

 = render 'forum'

 = link_to "Back", root_path

my index.html.erb under view>forums is completely empty. my application 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"

Anyone know how to solve this? If there is any code missing, please comment and I will add it.

Upvotes: 3

Views: 4590

Answers (1)

fl00r
fl00r

Reputation: 83680

I believe you wanted to write something like

Forum.new

not

forum.new

where forum is obviously is nil as far as you defined forum as

def forum
end

And I hope that you have defined Forum model in your application

Upvotes: 4

Related Questions