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

时间:2017-03-15 03:31:31

标签: ruby-on-rails

如何通过类别过滤我的条目?

我正在为了教育目的而使用reddit进行网页抓取,只抓取他们的标题,链接和类别。

以下是通过lib目录的代码:

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

尝试在index.html.erb中对此进行过滤,但不知道如何。

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

这是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 个答案:

答案 0 :(得分: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