Search code examples
controllerruby-on-rails-5

How is it I am unable to create a new index method?


I am attempting to add a new item to the to-do list. However, the new index is throwing an error. What am I missing?

_navbar.html.erb file:

<%= render "form_index %>

lists_controller.rb file:

def index
    @lists = List.all
    @list = List.new
   end

def new
    @list = List.new
  end

_form_index.html.erb file:

<div class="form-inline my-2 my-lg-0">
  <%= f.text_field :description, class: "form-control mr-sm-2" %>
  <%= f.submit "Add To-Do Item", class: "btn btn-outline-success my-2 my-sm-0" %>
<% end %>

My confusion resides within this error message:

undefined local variable or method `list' for #ActionView::Base:0x0000000002e2e8>

Did you mean? @list @lists

<%= form_with(mode: list) do |form| %>
   <% if list.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(list.errors.count, "error") %> prohibited this list from being saved:</h2>

Solution

  • I was missing a few @ signs and my text field was supposed to be set its true field which is "item"

    _form_index.html.erb file should have been:

    <div class="form-inline my-2 my-lg-0">
      <%= form.text_field :item, class: "form-control mr-sm-2" %>
      <%= form.submit "Add To-Do Item", class: "btn btn-outline-success my-2 my-sm-0" %>
    <% end %>
    

    The following error: undefined local variable or method `list' for #ActionView::Base:0x0000000002e2e8>

    Did you mean? @list @lists

    was telling me to go beyond the surface in my _form_index.html.erb file:

    <%= form_with(model: @list) do |form| %>
      <% if @list.errors.any? %>
        <h2><%= pluralize(list.errors.count, "error") %> prohibited this list from being saved:</h2>
    

    I am such a newbie and I love it more and more everyday. I have definitely learned to appreciate errors. I call often tell myself "read the screen".