2017-03-15 40 views
0

目前網上刮教育目的...我每次刪除.limit(25),在application_helper方法停止工作

我把限制在指數中的動作控制器25項...

entries_controller.rb

class EntriesController < ApplicationController 


def index 
    @entries = Entry.order("created_at DESC").limit(25) 
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 

試圖將其刪除,但每次我刪除了.limit(25),它開始說我cat(category)application_helper.rb是不工作...具體地說,它說:

NoMethodError in Entries#index 


undefined method `remove' for nil:NilClass 

這是application_helper文件:

module ApplicationHelper 

    def cat(category) 
     category.remove("https://www.reddit.com/r/").gsub(/\/$/, '') 
     #category.gsub(/https:\/\/www.reddit.com\/r\/?/, '').gsub(/\/$/, '') 
    end 

end 

爲什麼會發生呢?

我打電話的index.html.view

這裏cat(category)方法是index.html.erb

<div class="container"> 
<div class="card-columns"> 
    <% @entries.reverse.each do |entry| %> 
    <div class="card"> 
     <div class="card-block"> 
     <p class="card-title"><b><%= entry.title %></b></p> 
     <p class="card-text"><%= entry.link %></p> 
     <p class="card-type"><%= cat(entry.category) %></p> 
     </div> 
    </div> 
    <% end %> 
</div> 
</div> 

要再次重申我的問題,爲什麼cat(categeory)方法停止工作每次我從刪除.limit(25)時間索引行動?

如果需要更多說明,請讓我知道。

這是reddit_scraper.rb在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 

回答

0

這可能是有一個nil值在這些類別之一後,25

嘗試使用調試器

+0

是可能鑑於驗證是否存在鏈接和標題?此外,我已經提出了可以看到所有報廢代碼的報廢代碼... – user273072545345

+0

你是對的。我傾倒了數據庫的內容,再次耙刮板......沒有石灰就沒事了。所以,耶。謝謝。 – user273072545345

+0

歡迎你 –