5

Ok, the problem is trying to render a form from a controller to create a new entry.. For that im calling the default form that comes with the scaffold creation I'm trying to make it like this:

<%= render :partial => 'contactos/form'  %> 

And im getting the following error

undefined method 'model_name' for NilClass:Class

Is there any way of just rendering from the view itself?

If there is not... Which parameters should I add to the controller?

Right now I just have following code:

Class DisplayController < ApplicationController
     def index
        @contactos = Contacto.all
     end
end

*This is the view controller, not the one with the create update and edit functions from my scaffold

Ok, I've done a very large research but no answer can fix my problem. (This is the first time I ask something, sorry in advance for any mistake I could make)

mellamokb
  • 56,094
  • 12
  • 110
  • 136
Mau Ruiz
  • 767
  • 1
  • 8
  • 22

3 Answers3

6

The problem is that the variable your are using in the form for your contact doesn't exist. The only variable you created in the index action is an array of all the contacts, but the form needs a single instance of a single contact.

Because you are making a new contact, you have to do something like this in the action index:

@contact = Contact.new
sosborn
  • 14,676
  • 2
  • 42
  • 46
2

Generally this happens when we use an instance variable in the view but it is nil.

For eg: in new user creation form we use,

form_for @user do |f|
....
end

and @user is not initialized in the controller action, from where we render the form, this can happen.

nkm
  • 5,844
  • 2
  • 24
  • 38
0

you need to make sure that the param used in the form partial is defined before you render the partial.

Also you don't need the partial, you can just put:

<%= render 'contactos/form'  %> 
Matthew
  • 12,892
  • 6
  • 42
  • 45