Creating articles via the controller in Rails. A simple method, which more or less works; just call the method from some other place and it generates a new article via the back end and fills in the values:
def test_create_briefing
a = Article.new
a.type_id = 27
a.status = 'published'
a.headline = 'This is a headline'
a.lede = 'Our article is about some interesting topic.'
a.body = test_article_text
a.save!
end
If test_article_text
is just a single record, this works fine and prints the existing article body into the new article body. Looks right in the view and looks right in "edit". All perfect.
def test_article_text
a = Article.find_by_id(181)
a.body
end
But if I try to do the same thing with the last ten articles, it doesn't work:
def test_article_text
Article.lastten.each do |a|
a.body
end
end
In the view you get:
[#, #, #, #, #, #, #, #, #, #]
And in "edit" you get:
[#<Article id: 357, headline: "This is a headline", lede: "Our article is about some interesting topic.", body: "[#<Article id: 356, headline: \"This is a headline\"...", created_at: "2017-12-31 20:40:16", updated_at: "2017-12-31 20:40:16", type_id: 27, urgency: nil, main: nil, status: "published", caption: nil, source: nil, video: nil, summary: nil, summary_slug: nil, topstory: false, email_to: nil, notification_slug: nil, notification_message: nil, short_lede: nil, short_headline: nil, is_free: nil, briefing_point: nil>, #<Article id: 356, headline: "This is a headline"…etc, etc, etc.
What do I not know? What am I missing?
It is returned as below because the
Article.lastten
is the returned variable from your controller.To return all Article body, do as below:
So, @Shiko was nearly right, certainly on the right path. Had to manipulate the array a bit and do two things to get it to work:
.join
the bits of the array to strip out all of the rubbish;Concatenate the different bits for each article in a different way than you normally do in a view. So
to_s
for each of the attributes, concatenating things"" + ""
and rebuilding the url with information available in the array (nolink_to
, etc.).The
"**"
is markdown, because I'm using that, but I suppose you could bung html tags in there if you needed to.This works: