Hangloos Hangloos - 20 days ago 5
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!

Example:

`[#<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").html("")
$("#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

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.

Comments