1

我正在尝试从我的控制器操作中渲染一些东西。

def show

@bookmark = Bookmark.all
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @bookmark }
end
end

以上是控制器中的操作。

show 的 html erb 文件是:

<p>
<b>Url:</b>
<%= @bookmark.each do |book| %>
<li><%= book.url%>
<b>Title:</b>
<li><%= book.title%>
<% end %>
</p>

我也得到了输出,但除此之外,我得到了额外的东西。输出是:

Url:

www.google.com Title:
h
www.gmail.com Title:
hiw
asd Title:
as
sd Title:
ad
asd Title:
as
asd Title:
as
asfa Title:
ada
www.mrabhiram.github.com Title:
abhi
www.mrabhiram.github.com Title:
abhi 
www.mrabhiram.github.com Title:
abhi 


[#<Bookmark id: 1, title: "h", url: "www.google.com", tags: "php,python", created_at:    "2012-12-12 06:02:21", updated_at: "2012-12-12 06:02:21">, #<Bookmark id: 2, title: "hiw", url: "www.gmail.com", tags: "mail", created_at: "2012-12-12 06:02:41", updated_at: "2012-12-12 06:02:41">, #<Bookmark id: 3, title: "as", url: "asd", tags: "asd", created_at: "2012-12-12 06:07:41", updated_at: "2012-12-12 06:07:41">, #<Bookmark id: 4, title: "ad", url: "sd", tags: "dsfsd", created_at: "2012-12-12 06:11:04", updated_at: "2012-12-12 06:11:04">, #<Bookmark id: 5, title: "as", url: "asd", tags: "asd", created_at: "2012-12-12 06:22:36", updated_at: "2012-12-12 06:22:36">, #<Bookmark id: 6, title: "as", url: "asd", tags: "asda", created_at: "2012-12-12 06:25:33", updated_at: "2012-12-12 06:25:33">, #<Bookmark id: 7, title: "ada", url: "asfa", tags: "fa", created_at: "2012-12-12 07:41:16", updated_at: "2012-12-12 07:41:16">, #<Bookmark id: 8, title: "abhi", url: "www.mrabhiram.github.com", tags: "abhiram,php", created_at: "2012-12-12 07:45:29", updated_at: "2012-12-12 07:45:29">, #<Bookmark id: 9, title: "abhi", url: "www.mrabhiram.github.com", tags: "abhiram,php", created_at: "2012-12-12 07:46:19", updated_at: "2012-12-12 07:46:19">, #<Bookmark id: 10, title: "abhi", url: "www.mrabhiram.github.com", tags: "abhiram,php", created_at: "2012-12-12 07:48:28", updated_at: "2012-12-12 07:48:28">]

我最后应该摆脱数组打印。我只想打印记录。

4

1 回答 1

1

使用<% ... %>代替<%= ... %>

<% @bookmark.each do |book| %>

参见:eRB <%=

更新(回应评论):

如果只有一个书签,为避免出现错误,请将实例变量包装在Array()

<% Array(@bookmark).each do |book| %>

顺便说一句,我建议将您的实例变量从 重命名@bookmark@bookmarks

于 2012-12-12T08:19:50.543 回答