1
votes

I am always getting this error message:

NoMethodError in ListsController#new undefined method `fetch_value' for nil:NilClass

Extracted source (around line #8):

7 def new
8 @list = List.new
9 end

I don't get the reason for this error ^^

My routes.rb

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    root 'lists#index'

    get 'home' => 'lists#index', as: 'home'
    get 'new' => 'lists#new', as: 'new'

    resources :lists

end

Database name:

:lists


Model name:

list

lists_controller:

class ListsController < ApplicationController

    def index 
        @lists = List.all
    end

    def new
       @list = List.new
    end

    def show 
        @list = List.find(params[:id])
    end

    def create
        @list = List.new(list_params)

        if(@list.save)
            redirect_to @list
        else
            render 'new'
        end
    end

    def edit
        @list = List.find(params[:id])
    end

    def update
       @list = List.find(params[:id])

        if(@list.update(list_params))
            redirect_to @list
        else
            render 'edit'
        end
    end

    def destroy
       @list = List.find(params[:id]) 

        @list.destroy

        redirect_to lists_path
    end

    private def list_params
       params.require(:list).permit(:date, :class, :lesson, :subject, :teacher, :room, :info) 
    end
end

new.html.erb

<%= form_for :list, url: lists_path do |f| %>
    <% if @list.errors.any? %>
        <% @list.errors.full_messages.each do |error| %>
            <div class="alert alert-danger"><%= error %></div>
        <% end %>
    <% end %>

  <div class="alert alert-info">Please fill out all the fields below.</div>
    <p>
        <%= f.label :date %><br>
        <%= f.text_field :date, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.label :class %><br>
        <%= f.text_area :class, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.label :lesson %><br>
        <%= f.number_field :lesson, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.label :subject %><br>
        <%= f.text_field :subject, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.label :teacher %><br>
        <%= f.text_field :teacher, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.label :room %><br>
        <%= f.text_field :room, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.label :info %><br>
        <%= f.text_area :info, {:class => 'form-control'} %>
    </p>
    <p>
        <%= f.submit({:class => 'btn btn-info'}) %>
    </p>
<% end %>

Using the BootstrapCDN and only the default gems.

Thanks for any answers :)

2
I am not sure but can you once try removing the label and text area for the class attribute from the form and you should not use names like class which are already reserved. - Deepesh

2 Answers

2
votes

In your model you have an attribute named class which is a reserved keyword which is causing issues. As I can see your code:

<p>
    <%= f.label :class %><br>
    <%= f.text_area :class, {:class => 'form-control'} %>
</p>

So you should never use reserved keywords as attributes because you are overriding it in that case. So when overriding it, your attribute will not consist of all the properties required by the language. That is why you are getting the error.

1
votes

Rails is trying to fetch the attributes from :list which should be @list

Change the form_for in new.html.erb from

<%= form_for :list, url: lists_path do |f| %>

to

<%= form_for @list, url: lists_path do |f| %>