Rails: why can I send a single record attribute vi

2019-08-10 08:00发布

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?

2条回答
甜甜的少女心
2楼-- · 2019-08-10 08:11

It is returned as below because the Article.lastten is the returned variable from your controller.

[#<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.

To return all Article body, do as below:

def test_article_text
  arr = Array.new
  Article.lastten.each do |a|
    arr << a.body
  end
  arr # should be added so it will be the last value returned from your controller
end
查看更多
看我几分像从前
3楼-- · 2019-08-10 08:11

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:

  1. .join the bits of the array to strip out all of the rubbish;

  2. Concatenate the different bits for each article in a different way than you normally do in a view. So to_sfor each of the attributes, concatenating things "" + "" and rebuilding the url with information available in the array (no link_to, etc.).

  3. The "**"is markdown, because I'm using that, but I suppose you could bung html tags in there if you needed to.

This works:

def test_article_text
  arr = Array.new
  Article.lastten.each do |a|
  arr << "**" + a.headline.to_s + "**: " + a.text.to_s + "[Read now](/articles/#{a.id}-#{a.created_at.strftime("%y%m%d%H%M%S")}-#{a.headline.parameterize})"
  end
  arr.join("\n\n")
end
查看更多
登录 后发表回答