2017-03-15 77 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