通过Rails中的控制器创建文章。一种简单的方法,或多或少有效;只需从其他地方调用该方法,它就会通过后端生成一篇新文章并填写值:
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
只是一条记录,则可以正常工作并将现有的文章正文打印到新文章正文中。在视图中看起来正确并且在#34;编辑"中看起来正确。一切都很完美。
def test_article_text
a = Article.find_by_id(181)
a.body
end
但如果我尝试用最后十篇文章做同样的事情,那它就不起作用了:
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.
我不知道什么?我错过了什么?
答案 0 :(得分:0)
它返回如下,因为Article.lastten
是控制器返回的变量。
[#<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.
要返回所有文章正文,请执行以下操作:
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
答案 1 :(得分:0)
所以,@Shiko几乎是正确的,肯定是在正确的道路上。不得不稍微操纵数组并做两件事来让它工作:
function PostAction() {
$.ajax({
type: "POST",
dataType: "jsonp",
url:"https://moneywave.herokuapp.com/v1/merchant/",
headers: {
'apiKey': '12893n3m3ki3',
'secret':'jfjfjffjhfjfmk' },
success: function(data, textStatus, xhr){
// console.log("status",xhr.status);
},
complete: function(xhr, textStatus) {
console.log("http status",xhr.status);
}
});
}
数组的各个部分用于清除所有垃圾;
以与通常在视图中不同的方式连接每篇文章的不同位。因此,对于每个属性.join
,连接事物to_s
并使用数组中的可用信息重建网址(无"" + ""
等)。
link_to
是降价促销,因为我正在使用它,但我想如果你需要,可以在那里打开html标签。
这有效:
"**"