NoMethodError in Home # index

2

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

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

The persistent 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've already debugged, @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 portion of the view that calls partial:

<% if member_signed_in? %>

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

<% else %>

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 out, but it is not the ideal ...

<%= 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 %>

Change the @todo_list to TodoList.new. It worked, but I can not do it all the time; /.

Edit2

Function index:

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
    
asked by anonymous 01.08.2014 / 05:21

1 answer

0

The following syntax:

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

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

In your case you did this:

@todo_lists = ToDoList.all

This means that @todo_list is an instance of ActiveRecord::Relation , which means that it is a list of objects in that template, not in> a template only.

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

This is usually done as follows:

def index
  @modelos = Modelo.all
end

def new
  @modelo = Modelo.new
end

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

    
01.08.2014 / 17:54