Hangloos Hangloos - 1 year ago 61
Ajax Question

Why is an array of my rails objects showing up when using remote true?

I am showing the names of rails objects on a homepage using an ajax remote true link. The homepage will show the names properly but they are followed by an array of the objects. FYI, the code in my partial view has a helper method to add text to the recipe name.

This is a sample program for my school. Any ideas on what I can do to now show the array of objects? Thanks!


`[#<Recipe id: 46, name: "recipe 1", status: true, user_id: 9, created_at: "2016-11-01 16:34:27"]`

Code in my JS view:

var html = "<%= j render('recipes/recipespartial') %> "
$("#recipename").append("<%= j render('recipes/recipespartial') %> ")

Code in my partial view:

<%= current_user.recipes.each do |recipe| %>
<li><%= link_to recipe.name, recipe_path(recipe)%><%=status_changed_to_cooked_helper(recipe)%></li>
<% end %>

Link in my view:

<%= link_to "Show Recipes", recipes_path, :remote => true %>

Answer Source

Your partial should be

<% current_user.recipes.each do |recipe| %>
<% end %> 

Note that there is no = before current_user.recipes. In erb, any expression inside <% %> is evaluated and <%= %> is evaluated and interpolated into html. That is the reason you were seeing the array too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download