views:

686

answers:

1

Alright. I am working with RoR and jQuery and I have already got my update and create links working and functioning with their .js.erb files. The problem arises when in create with this line of code.

#create.js.erb
$("#promo_types").append("<%= escape_javascript(render(:partial => "promotion_type"))%>");

it is throwing an error: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each

#index.erb
<div id="promo_types">
  <%= render :partial => "promotion_type" %>
</div>

#_promotion_type
 <% @promotion_types.each do |@promotion_type| %>
<table>
    <tr>
      <td width="200"><%=h @promotion_type.name %></td>
      <td width="20"><a class="Edit<%=h @promotion_type.id %>" href="#"><img src="/images/pencil.png" alt="Edit" title="Edit"  /></a></td>
      <td width="20"><%= link_to(image_tag('/images/bin.png', :alt => 'Remove', :title => "Remove"), @promotion_type, :class => "deleteCategory") %></td>
    </tr>
    </table>
     <div id="popupEdit<%=h @promotion_type.id %>" class="popupEdit">
    <a class="popupClose<%=h @promotion_type.id %>" id="popupClose">x</a>
    <%= render :file => 'promotion_types/edit' %></div>
  <% end %>

It doesn't like that first line of code of the partial when I try to append the row using ajax.

My only other question is concerning my update.js.erb. I works fine. If i click edit and update it updates the changes, but I don't see them till I refresh the page. I don't want to .append the partial because the would add the edit to the bottom of the list. Do I use a .html to refresh my partial thru AJAX?

EDIT:

#full create.js.erb

$("#promo_types").append(<% escape_javascript(render(:partial => "promotion_type"))%>");
  $("new_promotion_type")[0].reset();
  $(".addtoggle01").toggle();
  $(".addtoggle01").before('<div id="notice">Your promotion type has been successfully submitted.</div>');

It resets my form, hides the form, and displays the notice, but it isn't appending anything and not throwing any errors either.

+1  A: 

In your _promotion_type partial you are looping through @promotion_types and it sounds like this instance variable is not getting set in the create action.

From the name, it sounds like that partial should only display one promotion type, not many. So move the loop outside of the partial. I recommend using the :collection and :object options to pass the model to the partial.

# index.html.erb
<div id="promo_types">
  <%= render :partial => "promotion_type", :collection => @promotion_types %>
</div>

# _promotion_type.html.erb
<table>
  <tr>
    <td width="200"><%=h promotion_type.name %></td>
    <td width="20"><a class="Edit<%=h promotion_type.id %>" href="#"><img src="/images/pencil.png" alt="Edit" title="Edit"  /></a></td>
    <td width="20"><%= link_to(image_tag('/images/bin.png', :alt => 'Remove', :title => "Remove"), promotion_type, :class => "deleteCategory") %></td>
  </tr>
</table>
<div id="popupEdit<%=h promotion_type.id %>" class="popupEdit">
<a class="popupClose<%=h promotion_type.id %>" id="popupClose">x</a>
<%= render :file => 'promotion_types/edit' %></div>

# create.js.erb
$("#promo_types").append("<%= escape_javascript(render(:partial => "promotion_type", :object => @promotion_type))%>");

As for your second question, yes. You can assign a specific ID to each record and replace that using the .html method.

# update.js.erb
$("#promo_type_<%= @promotion_type.id %>").html("<%= escape_javascript(render(:partial => "promotion_type", :object => @promotion_type))%>");
ryanb
i threw the loop outside the partial and I wasn't getting that error anymore. Thanks. But my .append and .html are having no effect at all. I'll make an edit it show you code.
Mike
Make sure to add the equal sign in `<%=` before escape_javascript.
ryanb
I think that was there. The code was on another computer so I'd was typing it over and might have overlooked it. I'll check tomorrow when I get back to work.
Mike
If that isn't the problem try something like FireBug to ensure there aren't any javascript errors. http://getfirebug.com/
ryanb
I use firebug all the time. Just got back to work and I was missing that = so that works now! Thanks a lot for all the help!
Mike