render partial issue

Hope you have any solution for this

I have a view with two partials:

<!-- LIST --> <div>        <p id="ppaypend">                <%= render :partial => 'list' %>        </p> </div> <!-- FORM --> <div id="form" style="display: none; ">        <%= render :partial => 'form' %> </div>

_list.html.erb partial code is here:

<table>        <thead>                <tr>                        <th>Service</th>                        <th>percentaje</th>                        <th>Amount</th>                        <th>date</th>                        <th>Select</th>                </tr>        </thead>        <tbody>        <% for payment in @payments do %>                <tr>                        <td><%= payment.service.description %></td>                        <td><%= payment.service.percentage %></td>                        <td><%= payment.amount %></td>                        <td><%= payment.created_at.to_s(:db) %></td>                        <td><%= link_to 'Select', edit_payment_path(payment), :remote => true %></td>                </tr>        <% end %>        </tbody> </table>

the idea of the code is when someone select any item in the table the form appears to show some information and then the user can update and submit their changes. After the submit the list need to be updated and I'm doing it with the following code in the update.js.erb file

$("#ppaypend").html("<%= escape_javascript( render :partial =>"list" ) %>");

(when i comment this line everything works but obviously the list does not show the changes)

in my controller i have this to assure the @payments is created every request:

class PaymentsController < ApplicationController before_filter :load

def load    @payments = Payment.joins(:application_form).where('application_forms.status' =>'pending')    @payment = Payment.new end ..... end

the error i have is:

ActionView::Template::Error (undefined method `each' for #<Payment: 0x10a3039e0>):    9: </tr>    10: </thead>    11: <tbody>    12: <% for payment in @payments do %>    13: <tr>    14: <td><%= payment.service.description %></td>    15: <td><%= payment.service.percentage %></td> app/views/payments/_list.html.erb:12:in `_app_views_payments__list_html_erb__1381777750_2232227020' app/views/payments/update.js.erb:3:in `_app_views_payments_update_js_erb___1521001921_2232244240' app/controllers/payments_controller.rb:45:in `update'

ActionView::Template::Error (undefined method `each' for #<Payment: 0x10a3039e0>): 9: </tr> 10: </thead> 11: <tbody> 12: <% for payment in @payments do %>

It's saying that @payments is a Payment, not an array of them. This means that your statement:

  @payments = Payment.joins(:application_form).where('application_forms.status' =>'pending')

is returning a single Payment object. I don't know why it's doing that -- maybe it's correct behavior in the case of one result, but it seems to me a one-element array would be "least surprising". As a workaround you could try immediately after that:

  @payments = [@payments] if Payment === @payments

Let us know how that works for you.

Alternately, are you absolutely sure that nothing else is modifying @payments? Since you have both singular and plural versions, it's quite plausible that you added an s on some line where you didn't mean to. Try searching the controller, view, and partials for @payments and objectively inspecting each occurrence.

-Dave

Also you can use ruby-debug to break into your code (after @payments = ... for example) and inspect the data to check it is correct. Similarly you can break into the code in the view to check it is still ok. Have a look at the Rails Guide on debugging to see how to do that, and also to see some other debugging techniques.

Colin