Ruby on rails Rails视图正在打印我们希望从散列中得到的某些值,然后是完整的散列-为什么?
我们试图构建一个提交评论和文章的提要,只调用并显示每个评论和文章的某些属性。我们可以获得各种提要,但页面也返回文章和评论的所有属性的散列。见下文: 控制器:Ruby on rails Rails视图正在打印我们希望从散列中得到的某些值,然后是完整的散列-为什么?,ruby-on-rails,view,hash,controller,Ruby On Rails,View,Hash,Controller,我们试图构建一个提交评论和文章的提要,只调用并显示每个评论和文章的某些属性。我们可以获得各种提要,但页面也返回文章和评论的所有属性的散列。见下文: 控制器: def show @user = User.find(params[:id]) # @feed= (Article.all + Comment.all).sort!{|a,b| a.updated_at <=> b.updated_at}.reverse.take(10) comments
def show
@user = User.find(params[:id])
# @feed= (Article.all + Comment.all).sort!{|a,b| a.updated_at <=> b.updated_at}.reverse.take(10)
comments = Comment.all
comments_array = []
comments.each do |f|
comment_info ={}
comment_info['feed_text'] = f.commenter + ' submitted to a goal'
comment_info['time_stamp']=f.updated_at
comments_array<<comment_info
end
articles=Article.all
articles_array =[]
articles.each do |f|
article_info ={}
article_info['feed_text']='A new goal has been created:' + f.title
article_info['time_stamp']=f.created_at
articles_array<<article_info
end
@feed = (comments_array + articles_array).sort!{|a,b| a['time_stamp'] <=> b['time_stamp']}.reverse.take(10)
end
2.向目标屈服
新的目标
已经创建了一个新目标:测试6
已经创建了一个新目标:test2
#what_we_don't_want:
[{“feed_-text”=>“2提交目标”,“time_-stamp”=>2014年7月31日星期四19:27:40 UTC+00:00},{“feed_-text”=>“新提交目标”,“time_-stamp”=>2014年7月31日星期四19:27:31 UTC+00:00},{“feed_-text”=>“已创建新目标:测试6”,“time_-stamp”=>2014年7月31日星期四19:27:19+00:00},{“feed_-text”=>已创建新目标:测试2,“时间戳”=>2014年7月31日星期四14:45:09 UTC+00:00}]
我们无法理解页面为什么同时显示“我们想要什么”和“我们不想要什么”。您想替换吗
<%= @feed.each do |f| %>
与
#what_we_don't_want:
<%= @feed.each do |f| %>
<% @feed.each do |f| %>