Rails在控制器中查看逻辑,生成散列/数组而不是打印数据

时间:2016-03-08 12:06:06

标签: ruby-on-rails model-view-controller

尝试以编程方式执行摘要“文章”,充满了过去24小时内的“newsitems”。目前,表单上的提交按钮指向create_summary,它可以在文章控制器中找到它:

def create_summary
  @article = Article.create!(
  headline: 'SUMMARY: Spain March 8, 2016',
  type_id: '27',
  status: 'published',
  body: Newsitem.last24.each do |newsitem|
    newsitem.slug
  end
  )
  respond_to do |format|
    format.html { redirect_to :action => 'admin', notice: 'Article was successfully created.' }
  end
end

它创建新文章没关系,Newsitem.last24.each do返回正确数量的newsitems,但是作为哈希或数组,而不是让我做你通常在一个块中做的事情。这是它返回的哈希/数组:

[#<Newsitem id: 173, item: "Lorem ipsum dolor sit amet, consectetur adipiscing...", source: "", created_at: "2016-03-07 16:15:46", updated_at: "2016-03-07 16:25:42", slug: "Test Draft Live Blog Update With A Photo. Please I...", url: "", main: nil, imagesource: "A place.", status: "published", caption: "A test video caption with some words.", article_id: 165, video: "w5zJU2UP-So">, #<Newsitem id: 159, item: "The former long-time Popular Party Mayor of Valenc...", source: "", created_at: "2016-03-07 22:47:33", updated_at: "2016-03-07 22:48:50", slug: "New Test Update With Image Long Headline", url: "", main: "Screen_Shot_2015-12-23_at_21.04.48.png", imagesource: "", status: "published", caption: "", article_id: nil, video: "">, #<Newsitem id: 174, item: "The former long-time Popular Party Mayor of Valenc...", source: "", created_at: "2016-03-08 09:00:34", updated_at: "2016-03-08 09:00:34", slug: "A Test Last 24 Update", url: "", main: nil, imagesource: "", status: "published", caption: "", article_id: nil, video: "">]

我们在视图和部分中可以做的通常是:

<% Newsitem.last24.each do |newsitem| %>
 **<%= newsitem.slug.upcase %>:** <%= newsitem.item.truncate(265) %>
 (<%= link_to 'Read full update', newsitem %>)
<% end %>

...这样文章的正文中填充了过去24小时内的一个很好的新网站列表。逻辑和查询在视图中工作,但我无法弄清楚如何通过控制器将相同的东西放入文章的“正文”中。

选项2:

还尝试在create_summary上通过控制器渲染部分内容。这几乎可行。替代代码:

def summary_body
  render :partial => 'newsitems/last24'
end

def create_summary
  @article = Article.create!(
  headline: 'SUMMARY: Spain March 8, 2016',
  type_id: '27',
  status: 'published',
  body: summary_body
  )
  respond_to do |format|
    format.html { redirect_to :action => 'admin', notice: 'Article was successfully created.' }
  end
end

仍然返回一个哈希值,但至少进行了一些打印和格式化:

["              **TEST DRAFT LIVE BLOG UPDATE WITH A PHOTO. PLEASE IGNORE.:** Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce accumsan neque ac leo varius, sit amet lobortis lectus posuere. Ut ut felis bibendum metus placerat rhoncus sit amet id enim. Integer eget metus in ligula bibendum blandit sed et justo. Cras cursus d...\n              (<a href=\"http://localhost:3000/articles/165-160227161454-spanish-congress-votes-219-131-against-pedro-sanchez-as-new-pm-in-second-vote#173\">Read full update</a>)\n              **NEW TEST UPDATE WITH IMAGE LONG HEADLINE:** The former long-time Popular Party Mayor of Valencia, Rita Barberá, denied during a press conference on Thursday morning that she had ever committed fraud during her time at City Hall. &quot;I have not contributed to, ordered or ever known about money laundering or h...\n              (<a href=\"/newsitems/159-160307234733-update-new-test-update-with-image-long-headline\">Read full update</a>)\n              **A TEST LAST 24 UPDATE:** The former long-time Popular Party Mayor of Valencia, Rita Barberá, denied during a press conference on Thursday morning that she had ever committed fraud during her time at City Hall. &quot;I have not contributed to, ordered or ever known about money laundering or h...\n              (<a href=\"/newsitems/174-160308100034-update-a-test-last-24-update\">Read full update</a>)\n"]

所以第二个版本中的问题是以某种方式分解或解析格式化的数组。

如何从A到B(在任何一种情况下)?

2 个答案:

答案 0 :(得分:0)

根据文档,eachhttp://ruby-doc.org/core-2.2.0/Array.html#method-i-each)上的Array方法会返回正确的Array。因此,您无法进行array.element之类的操作,因为Arrays是带编号的索引数据结构,因此您可以访问类似array[0].upcase的内容。
然而,你可以做的是设置身体接收:

 body: Newsitem.last24

这样,bodyActiveRecord::Relation,因此您可以对其进行迭代,并使用您喜欢的哈希/对象表示法。

答案 1 :(得分:0)

最后回答了这个问题,here

您必须在控制器中以与在视图中不同的方式处理相同的数组。不确定为什么Rails会以这种方式工作,或者是否与MVC有关。