2017-03-15 47 views
1

如何通过类别过滤条目?如何按类别筛选条目...范围或其他方式?

我webscrapping reddit的教育目的,只刮他们的标题,链接和类别。

这里是它通过lib目录代码:尝试如何在index.html.erb,但在亏损过滤此

require 'open-uri' 

module RedditScrapper 
    def self.scrape 
    doc = Nokogiri::HTML(open("https://www.reddit.com/")) 

    entries = doc.css('.entry') 
    entries.each do |entry| 
     title = entry.css('p.title > a').text 
     link = entry.css('p.title > a')[0]['href'] 
     category = entry.css('p.tagline > a.subreddit')[0]['href'] 
     Entry.create!(title: title, link: link, category: category) 
    end 
    end 

end 

决定尝试范围,即使我以前从未写过。

下面是Entry模型

class Entry < ApplicationRecord 

    validates :title, presence: true 
    validates :link, presence: true 
    validates :category, presence: true 

    scope :categorization, -> (category) { where category: category } 

end 

范围将范围上的索引行动entries_controller

class EntriesController < ApplicationController 


def index 
    @entries = Entry.all 
    @entries = @entries.categorization 
end 

def scrape 

    RedditScrapper.scrape 

    respond_to do |format| 
     format.html { redirect_to entries_url, notice: 'Entries were successfully scraped.' } 
     format.json { entriesArray.to_json } 
    end 
    end 

end 

收到此错误信息,指出:

ArgumentError in EntriesController#index 
wrong number of arguments (given 0, expected 1) 

,它的参考到范围。

我该如何解决这个问题,或者通过分类筛选项,如果有一个不同的方式去做,而不是作用域。

在此先感谢,并请让我知道是否需要额外的数据。

输出

LifeProTips 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

LPT: if you're in a bad mood or something has annoyed you, be as kind as you can to the next 3 people you encounter. By not reacting to the bad mood, as well as helping others, you'll feel miles better. 

/r/LifeProTips/comments/5zf0l2/lpt_if_youre_in_a_bad_mood_or_something_has/ 

回答

1

的样本您不要在这里需要一个范围,他们清理where条款。只需使用group_by

def index 
    @entries = Entry.all.group_by{ |entry| entry.category } 
    # a shorter more ruby way of writing this is: 
    # @entries = Entry.all.group_by(&:category) 
end 

,这将创造这样一个哈希:

{ 
    "category1" => [Entry,Entry,Entry], 
    "category2" => [Entry], 
    ... 
} 

然后在视图中可以

在刮板
<% @entries.each do |category, entries| %> 
    <h1><%= category %></h1> 
    <% entries.each do |entry| %> 
    <p><%= entry.inspect %></p> 
    <% end %> 
<% end %> 

你的第二个问题不处理重复:

更改:

Entry.create!(title: title, link: link, category: category) 

到:

Entry.where(title: title, link: link, category: category).first_or_create 
+0

谢谢你的答案... ...它的工作原理有点......有点摸不着头脑,输出不断重演......已经张贴在后的样本上方..任何理论? – user273072545345

+0

另外,我想知道是否有可能列出所有可点击类别的侧栏,所以当您点击它时,它会对条目进行排序以仅显示这些条目......或者是另一个问题帖子吗? – user273072545345

+0

有趣......我又看了一遍......结果并不只是重复它......它实际上是每个单独的数据库条目......我猜是因为我每两分钟就设置一次刮板,所以它不断重复,对吧?因为即使我尝试了'uniq' ...仍然没有解决它...这里是我在索引行动'@lala = @ entries.uniq.group_by(&:category)'' – user273072545345