我使用这种方法来挑选类似的帖子,我已经把它放在post.rb
。现在我遇到两个问题1.我收到一个错误。 2.如何将我落实到视图或控制器这得到类似的职位?:类似的帖子在rails
class Post < ActiveRecord::Base
def similar_posts(post)
title_keywords = post.title.split(' ')
Post.all.to_a.sort do |post1, post2|
post1_title_intersection = post1.body.split(' ') & title_keywords
post2_title_intersection = post2.body.split(' ') & title_keywords
post2_title_intersection.length <=> post1_title_intersection.length
end[0..9]
end
end
我得到的错误:
syntax error, unexpected ',', expecting keyword_end Post.all.to_a.sort |post1, post2|^
syntax error, unexpected keyword_end, expecting end-of-input
IV使用支架生成上岗一个简单的应用程序,如何将我实现这个展现类似的帖子?谢谢 –
@ user5724342这取决于你希望看到什么,以及在哪里 –
我希望在展会上查看每个帖子和标题。 Iv将代码实现到我的模型中,但我不确定如何在我的视图中使用它? –