Nomethoderror in Home#index

Asked

Viewed 334 times

2

Next, I received a challenge to create a page with login, using Sign in and with the user logged in, that he could create a task list, an to-do list.

The login I managed to do and works well, already create the part of the posts this being the biggest problem. created some files, the model, the controler, a partial and the view that should call this partial.

The insistent error is:

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
2: <%= form_for(@todo_list) do |f| %>
3:     <% if @todo_list.errors.any? %>
4:         <div id="error_explanation">
5:           <h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited this todo_list from being saved:</h2>

I already debugged, the @todo_list comes null, I do not know why, since apparently everything is well declared and instantiated.

Here’s the model:

class ToDoList < ActiveRecord::Base attr_accessible :is_favorite, :name, :description has_many :tasks, dependent: :destroy belongs_to :member end

the controller:

`class ToDoListsController < ApplicationController

  def index
...
  def show
...
  end

  def new

        @todo_list = ToDoList.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @todo_list }
     end`

the section of the view that calls the partial:

<% if member_signed_in? %>

    <%= render partial: 'to_do_lists/to_do_list' %>
    <%= link_to "Saída", destroy_member_session_path, :method => :delete %>

<% else %>

the routes:

new_member_session GET    /members/sign_in(.:format)       devise/sessions#new
            member_session POST   /members/sign_in(.:format)       devise/sessions#create
    destroy_member_session DELETE /members/sign_out(.:format)      devise/sessions#destroy
           member_password POST   /members/password(.:format)      devise/passwords#create
       new_member_password GET    /members/password/new(.:format)  devise/passwords#new
      edit_member_password GET    /members/password/edit(.:format) devise/passwords#edit
                           PUT    /members/password(.:format)      devise/passwords#update
cancel_member_registration GET    /members/cancel(.:format)        registration#cancel
       member_registration POST   /members(.:format)               registration#create
   new_member_registration GET    /members/sign_up(.:format)       registration#new
  edit_member_registration GET    /members/edit(.:format)          registration#edit
                           PUT    /members(.:format)               registration#update
                           DELETE /members(.:format)               registration#destroy
                 dashboard        /dashboard(.:format)             home#dashboard
                  register        /register(.:format)              registration#register
                      root        /                                home#index

Edit: One thing worked, but it’s not the idea...

<%= form_for(@todo_list = ToDoList.new) do |f| %> 
<%#= form_for(@todo_list) do |f| %>
    <% if @todo_list.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited this todo_list from being saved:</h2>

          <ul>
            <% @todo_list.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

Changing @todo_list to Todolist.new. It worked, but I can’t do it all the time ;/.

Edit2

Index function:

class ToDoListsController < ApplicationController
  def index
    @todo_lists = ToDoList.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @todo_lists }
    end
  end
end
  • 1

    What is the content of the index method of the class Todolistcontroller?

  • As @Bernardo said, the error is in the action index and you just posted the code of new

  • 1

    sorry guys, the code: class ToDoListsController < ApplicationController&#xA;&#xA; def index&#xA; @todo_lists = ToDoList.all&#xA;&#xA; respond_to do |format|&#xA; format.html # index.html.erb&#xA; format.json { render json: @todo_lists }&#xA; end&#xA; end.

1 answer

0


The following syntax:

<%= form_for @modelo do |f| %>
  ...
<% end %>

It only works if @modelo is an instance of ActiveRecord::Base.

In your case you did it:

@todo_lists = ToDoList.all

It means that @todo_list is an instance of ActiveRecord::Relation, which means he is a list objects of that model, and not a model only.

When you used @todo_list = ToDoList.new it started working because now yes you have only one model.

It’s usually done this way:

def index
  @modelos = Modelo.all
end

def new
  @modelo = Modelo.new
end

And we create in new.html.erb the registration form and index.html.erb we present all models (within a <table>, for example).

  • Yes, I went on to do the new and the index to work and abandoned the idea of the partial I had. I was told to try using partials when I’m firmer. But how can I have the login already ready and the to-do list on the same page if I can’t use partial?

  • @Amancio Do you want to have the whole list and still the form on the same page? You can instantiate the two variables in the same action/method: @modelo = Modelo.new and @modelos = Modelo.all. Use @modelos to display the list and @modelo for the form.

  • Yes. The idea is to log in, to look like the list creation form, what has already been created and an exit button to scroll down. Then I create my @template in the controller "modelo_controller" and then just call to another controller?

  • Would the login page have to be on the same page? For me it makes sense that you have two page, one login (page and controller of the Devise itself, usually) and another with the list and the form (todo_list_controller). I don’t understand what you mean by "another controller", the form and list will be on index and the form will point to create which then redirects back to the index.

  • I have three controllers. One from Devise, One called home and the other from to_do_lists. Hm, I think I figured out where I got stuck. There was no need for me to create an extra controller for to_do_list if I had the home one, which is where the list and the form should be. Out that after logged in the user is already played for home. Thanks for the help!

  • @You’re welcome to help. Have a nice weekend! =)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.