0

I use this code in my app\views\pages\home.html.erb file

<%= render :template => 'users/new' %>

I use this code in my app\views\users\new.html.erb file

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

Showing the "Home Page" I get this error:

NoMethodError in Pages#home

undefined method `model_name' for NilClass:Class

Extracted source (around line #1):

1: <%= form_for(@user) do |f| %>

2: <% if @user.errors.any? %>

...

I read this, but it does not work. What can I do to render template with "sub-partial" correctly?

Community
  • 1
  • 1
user502052
  • 14,803
  • 30
  • 109
  • 188

1 Answers1

1

In your PagesController#home() method, you'll need to set @user -- probably with:

@user = User.new
Nate
  • 4,561
  • 2
  • 34
  • 44
  • Thanks, it work. I founded that you can make the same thing with: <%= render :template => 'users/new', :object => @user = User.new %> in the home.html.erb file. – user502052 Dec 09 '10 at 18:20
  • Yeah -- it's generally cleaner to do your object creation in your controllers than your views, though. – Nate Dec 09 '10 at 18:25
  • 1
    Partials are more reusable when you don't work with instance variables though, so even cleaner would be `<%= render "form", :user => @user %>` and in the partial do something like `<%= form_for user do |f| %>` (not using the ivar) – iain Dec 09 '10 at 18:40