使用远程true时,为什么会显示我的rails对象数组? [英] Why is an array of my rails objects showing up when using remote true?

查看:105
本文介绍了使用远程true时,为什么会显示我的rails对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ajax远程true链接在主页上显示rails对象的名称.主页将正确显示名称,但它们后面是对象数组.仅供参考,我的局部视图中的代码有一个辅助方法,可将文本添加到配方名称中.

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"]`

我的JS视图中的代码:

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_to "Show Recipes", recipes_path,  :remote => true %>

推荐答案

您的部分应该是

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

请注意,current_user.recipes之前没有=.在erb中,对<% %>中的任何表达式进行求值,并对<%= %>进行求值并将其插入到html中.这就是您也看到数组的原因.

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.

这篇关于使用远程true时,为什么会显示我的rails对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆