2016-03-08 28 views
-1

試圖以編程方式執行摘要「文章」,其中包含過去24小時內的「newsitems」。目前,一個在形狀點提交按鈕create_summary,它可以讓你這個文章控制器內部:Rails在控制器中生成散列/數組而不是打印數據的控制器中查看邏輯

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(無論哪種情況)?

回答

0

終於回答了這個問題,又來了,here

您必須以不同的方式在控制器中處理相同的數組,而不是在視圖中進行處理。不確定爲什麼Rails會以這種方式工作,或者一般情況下是否與MVC有關。

0

根據文檔,Arrayhttp://ruby-doc.org/core-2.2.0/Array.html#method-i-each)上的each方法返回正確的Array。因此,您無法執行像array.element這樣的操作,因爲Arrays是編號索引的數據結構,所以您可以使用類似array[0].upcase的內容進行訪問。
什麼但是你可以做的是設置身體接受:

body: Newsitem.last24 

這樣,bodyActiveRecord::Relation,這樣你就可以在它迭代,並使用你喜歡的散列/對象符號。

+0

嗯,接近,我該怎麼辦呢?我在哪裏迭代他們?將它縮小到僅僅'body:Newsitem.last24'就會在創建的文章的主體中返回'#'。 –

+0

@MatthewBennett是的。在你看來,你可以做'body.each do | newsitem |'。每個'newsitem'現在都是一個'NewsItem'模型對象,所以你可以使用'newsitem.slug.upcase'等。 – MurifoX

+0

作爲文章視圖中條件的一部分? –