Probelm with partials

I have this in my view:

<h1>New client</h1>

<%= error_messages_for :client %> <%= error_messages_for :entity %>

<% form_for(@client) do |f| %>

  <% render :partial => "entities/entity_fields_for", :object => "@entity" %>

  <p>     <b>Client status</b><br />     <%= f.text_field :client_status %>   </p>

  <p>     <b>Client credit policy</b><br />     <%= f.text_field :client_credit_policy %>   </p> ...

I have this in entities/_entity_fields_for.html.erb

<h3> Entity Header </h3> <% fields_for(@entity) do |e| %>     <p>       <b>Entity Name</b><br />       <%= e.text_field :entity_name %>     </p>

    <p>       <b>Entity Legal Name</b><br />       <%= e.text_field :entity_legal_name %>     </p>

    <p>       <b>Entity Legal Form</b><br />       <%= e.text_field :entity_legal_form %>     </p>

  <% end %>

When I try and add a new client I only see this:

New client XXXXXXXXXXXXXXXXXXXXXXXX

Client status XXXXXXXXXXXXXXXXXXXXXXXX

Client credit policy XXXXXXXXXXXXXXXXXXXXXXXX

Client credit terms XXXXXXXXXXXXXXXXXXXXXXXX

Effective from XXXX XXXXXXXX XX

Superseded after XXXX XXXXXXXX XX

Back

and in the log I see this:

Processing ClientsController#new (for 127.0.0.1 at 2008-03-14 15:13:12) [GET]   Session ID: BAh7BzoMY3NyZl9pZCIlODdiM2M2YWNlZWVmZTFmYTgzMDk5MzQ2ODI0MWI2%0AYzAiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D--183ba94d00e732557171e89ef1b916a18f66701d   Parameters: {"action"=>"new", "controller"=>"clients"} Rendering template within layouts/clients Rendering clients/new Rendered entities/_entity_fields_for (0.00370) Completed in 0.05382 (18 reqs/sec) | Rendering: 0.01708 (31%) | DB: 0.00000 (0%)

So what obvious ( to others) thing am I leaving out or not doing with respect to displaying the partial? If the code that is in the partial is left in the new template then it displays as expected.

one error that look obvious is that: <% render :partial => "entities/entity_fields_for", :object => "@entity" %>

should be: <% render :partial => "entities/entity_fields_for", :object => @entity %>

without those quotes arount @entity

Thorsten Mueller wrote:

one error that look obvious is that: <% render :partial => "entities/entity_fields_for", :object => "@entity" %>

should be: <% render :partial => "entities/entity_fields_for", :object => @entity %>

without those quotes arount @entity

The presence or absence of "" around the instance variable does not change the observed behaviour. The fields are still left un-rendered. In any case, there is a constant present in the partial, <h3> Entity Header </h3>, which is not displayed either and whose display is independent of any values passed to the partial template. The problem is that the log says that the partial is being rendered and yet none of its contents, not even the label and heading text, are present in the resulting html document sent to my browser.

right, but a closer look:

use <%= %> instead of <% %>

Thorsten Mueller wrote:

right, but a closer look:

use <%= %> instead of <% %>

Where? The partial uses <%= %> already. What else needs to have the ruby generated code put into the document?

James Byrne wrote:

Thorsten Mueller wrote:

right, but a closer look:

use <%= %> instead of <% %>

Where? The partial uses <%= %> already. What else needs to have the ruby generated code put into the document?

Got it. Thanks.

I have this in my view:

<h1>New client</h1>

<%= error_messages_for :client %> <%= error_messages_for :entity %>

<% form_for(@client) do |f| %>

<%= render :partial => "entities/entity_fields_for", :object => @entity %>

<p>    <b>Client status</b><br />    <%= f.text_field :client_status %> </p>

<p>    <b>Client credit policy</b><br />    <%= f.text_field :client_credit_policy %> </p> ...

I have this in entities/_entity_fields_for.html.erb

<h3> Entity Header </h3> <% fields_for(@entity) do |e| %>

fields_for(entity_fields_for) do |e|

By using :object, you're telling the partial which object the local that is the name of the partial (i.e., entity_fields_for) refers.

   <p>      <b>Entity Name</b><br />      <%= e.text_field :entity_name %>    </p>

   <p>      <b>Entity Legal Name</b><br />      <%= e.text_field :entity_legal_name %>    </p>

   <p>      <b>Entity Legal Form</b><br />      <%= e.text_field :entity_legal_form %>    </p>

<% end %>

When I try and add a new client I only see this:

New client XXXXXXXXXXXXXXXXXXXXXXXX

Client status XXXXXXXXXXXXXXXXXXXXXXXX

Client credit policy XXXXXXXXXXXXXXXXXXXXXXXX

Client credit terms XXXXXXXXXXXXXXXXXXXXXXXX

Effective from XXXX XXXXXXXX XX

Superseded after XXXX XXXXXXXX XX

Back

and in the log I see this:

Processing ClientsController#new (for 127.0.0.1 at 2008-03-14 15:13:12) [GET] Session ID: BAh7BzoMY3NyZl9pZCIlODdiM2M2YWNlZWVmZTFmYTgzMDk5MzQ2ODI0MWI2%0AYzAiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D--183ba94d00e732557171e89ef1b916a18f66701d Parameters: {"action"=>"new", "controller"=>"clients"} Rendering template within layouts/clients Rendering clients/new Rendered entities/_entity_fields_for (0.00370) Completed in 0.05382 (18 reqs/sec) | Rendering: 0.01708 (31%) | DB: 0.00000 (0%)

So what obvious ( to others) thing am I leaving out or not doing with respect to displaying the partial? If the code that is in the partial is left in the new template then it displays as expected.

The presence or lack or quotes in the render line have no effect because you specifically used @entity within the partial anyway and never used the local variable that you set up with :object (which gets the name of the partial every time).

Thorsten's comments about <% vs. <%= are important, too, of course.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com