Problem rendering partial in the application layout

Here’s the call in the application.html.erb file:

<%= render :partial => ‘tasks/_new’ %>


Here’s the partial being rendered (_new.html.erb):

<% form_for @task do |f| -%>
<%= f.text_field :body %>
<%= submit_tag “Submit” %>
<% end -%>


Here’s the method in the ‘tasks’ controller:

def new
@task = Task.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @task }
end
end


Here’s the error message I keep getting:

Missing template tasks/__new.erb in view path app/views

And it says the error is in this line:

<%= link_to “tasks”, tasks_path %> <%= render :partial => ‘tasks/_new’
%>


The file is in the right directory. The weird thing is that there’s an
extra “_” in the file name, in the error. When I give in and rename the
partial to ‘__new.erb’, here’s the error I get:

Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id

And it says the error is in this line:

<% form_for @task do |input| -%>


What’s goin on?

PS:

<% form_for @task do |input| -%>

This actually reads “<% form_for @task do |f| -%>”.

Also, I also tried it without the _, as “<%= render :partial =>
‘tasks/new’ %>”, and I get the “Called id for nil” error.

Ralph W. wrote:

PS:

<% form_for @task do |input| -%>

This actually reads “<% form_for @task do |f| -%>”.

Also, I also tried it without the _, as “<%= render :partial =>
‘tasks/new’ %>”, and I get the “Called id for nil” error.

“tasks/new” is the correct syntax. So the “Called id for nil” is
probably coming from a method call in the partial. Time for testing and
debugging, now that the partial is getting called!

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Ralph W. wrote:

Marnen Laibow-Koser wrote:
“tasks/new” is the correct syntax. So the “Called id for nil” is
probably coming from a method call in the partial. Time for testing and
debugging, now that the partial is getting called!

Thanks, the problem was that there was no instance of ‘@task’ to be
called. I changed the code in the partial to “<% form_for Task.new do
|f| -%>” and it worked.

However, is this the more appropriate solution? Looks kinda like a hack
to me.

The most appropriate solution would be to use :locals to pass the
variable. See the render docs.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Ralph W. wrote:

Marnen Laibow-Koser wrote:
“tasks/new” is the correct syntax. So the “Called id for nil” is
probably coming from a method call in the partial. Time for testing and
debugging, now that the partial is getting called!

Thanks, the problem was that there was no instance of ‘@task’ to be
called. I changed the code in the partial to “<% form_for Task.new do
|f| -%>” and it worked.

However, is this the more appropriate solution? Looks kinda like a hack
to me.

The most appropriate solution would be to use :locals to pass the
variable. See the render docs.

Wait, I just realized something else. You’re calling the partial from
the application layout, rather than from a controller action. Why?

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

The most appropriate solution would be to use :locals to pass the
variable. See the render docs.

I’m trying to get that working.

Wait, I just realized something else. You’re calling the partial from
the application layout, rather than from a controller action. Why?

I want it to be in the application layout, across all pages (like a
site-wide search box), and thought the right way to do this was to use a
partial for the appropriate action from tasks controller.

Marnen Laibow-Koser wrote:
“tasks/new” is the correct syntax. So the “Called id for nil” is
probably coming from a method call in the partial. Time for testing and
debugging, now that the partial is getting called!

Thanks, the problem was that there was no instance of ‘@task’ to be
called. I changed the code in the partial to “<% form_for Task.new do
|f| -%>” and it worked.

However, is this the more appropriate solution? Looks kinda like a hack
to me.

Ralph W. wrote:

The most appropriate solution would be to use :locals to pass the
variable. See the render docs.

I’m trying to get that working.

Good.

Wait, I just realized something else. You’re calling the partial from
the application layout, rather than from a controller action. Why?

I want it to be in the application layout, across all pages (like a
site-wide search box), and thought the right way to do this was to use a
partial for the appropriate action from tasks controller.

That’s one way to do it, yes (but remember that in this case, it could
be rendered from any controller, not just Tasks). Of course, if you
have any variables called in the partial, then you have to make sure
they’re set. In this case, you should probably do that in a
before_filter in ApplicationController.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]