Abhi Ram A
Abhi Ram A

Reputation: 305

Rendering variable in Ruby on Rails not correct

I am trying to render something from my controller action.

def show

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

This above is the action in the controller.

The html erb file for show is :

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

I am getting output too, but additional to that I am getting extra things. The output is :

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

I should get rid of the array printing at last. I only want the records to be printed.

Upvotes: 1

Views: 105

Answers (1)

Chris Salzberg
Chris Salzberg

Reputation: 27374

Use <% ... %> instead of <%= ... %>:

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

See: eRB <%=

Update (in response to comments):

To avoid an error if there is only one bookmark, wrap the instance variable in Array():

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

Incidentally I would suggest renaming your instance variable from @bookmark to @bookmarks.

Upvotes: 1

Related Questions