Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a form from another controller in Ruby on Rails 3

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)

like image 611
Mau Ruiz Avatar asked Oct 13 '11 04:10

Mau Ruiz


People also ask

What is the difference between redirect and render in Ruby on Rails?

There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.

How can you tell Rails to render without a layout?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

What are partials Ruby?

Ruby on Rails Views Partials Partial templates (partials) are a way of breaking the rendering process into more manageable chunks. Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates.


1 Answers

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
like image 167
sosborn Avatar answered Nov 14 '22 22:11

sosborn