'.each do' is leaving some line like "#<Table:0x224af70>"
I'm trying to prepare a json object for my rails app. Here is my code:
#videos_controller
def show
@video = Video.find(:all,
:conditions => { :published => true, :trash => false },
:order => 'RANDOM()',开发者_StackOverflow中文版 :limit => 1)
respond_to do |format|
format.html # show.html.erb
format.json {render :partial => "videos/show.json"}
end
end
#_show.json
<%= @video.each do |video| %>
{
"video_link": "<%= video.link %>",
"video_id": "http://website.com/videos/each/<%=video.id%>"
}
<% end %>
but at videos/show.json I'm getting something like that
{
"video_link": "http://www.youtube.com/watch?v=6rmWnwtps6I",
"video_id": "http://website.com/videos/each/51"
}
#<Video:0x220a060>
How to avoid the nasty last line and were does it getting from? I think, becouse of that, it doesn't allow me to work with json object properly. I know, that answer is pretty simple, but just can't get it. Thank you in advance.
Take out the equal sign in the line <%= @video.each do |video| %>
so it's just <% @video.each do |video| %>
. The segment you're seeing is the default to_s method which is being returned from the each method.
Change this:
<%= @video.each do |video| %>
to this:
<% @video.each do |video| %>
Since you’re outputting text in the body of the loop, you don’t want to output the loop itself (the result of which is the enumerable itself).
精彩评论