Rails:为什么我可以通过控制器将单个记录属性发送到新记录,而不发送到数组? [英] Rails: why can I send a single record attribute via controller to new record but not array?

查看:53
本文介绍了Rails:为什么我可以通过控制器将单个记录属性发送到新记录,而不发送到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Rails中的控制器创建文章.一种简单的方法,或多或少有效;只需从其他地方调用该方法,它就会通过后端生成新文章并填写值:

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

如果test_article_text只是一条记录,则可以正常工作并将现有文章正文打印到新文章正文中.在视图中看起来正确,在编辑"中看起来正确.一切完美.

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

在视图中,您得到:

[#, #, #, #, #, #, #, #, #, #]

然后在编辑"中得到:

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

推荐答案

因此,@ Shiko几乎正确,当然也正确.必须稍微操作一下数组并做两件事才能使其工作:

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数组中的位将所有垃圾清除掉;

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

以与通常在视图中不同的方式来连接每篇文章的不同位.因此,对于每个属性to_s,将事物"" + ""连接起来,并使用数组中可用的信息(无link_to等)重建URL.

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.).

"**"是markdown,因为我正在使用它,但是我想如果需要的话,您可以在其中塞上html标记.

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

这有效:

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

这篇关于Rails:为什么我可以通过控制器将单个记录属性发送到新记录,而不发送到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆