我想弄清楚如何使我的类别可以链接到他们的条目。
我只有一个模型,Entry
从Reddit中删除了数据(标题,链接和类别)
现在,index.html.erb
页面上的数据与侧栏中的类别一起显示......但它没有链接。
如何将其链接到它来自的条目?
我提出的最多的内容也在index.html.erb
中,但它已落后了,因为我不确定如何调用我点击列的特定条目...也就是说,如果我点击笑话,我想得到所有具有该特定类别的条目......
这是不完整的link_to <p><%= link_to '@lala.join(" ")', :action %></p>
我在上面的路径中输入[:category]的参数吗?
条目控制器
class EntriesController < ApplicationController
def index
@entries = Entry.all
@lala = @entries.select(:category).map(&:category).uniq
#@entries = Entry.all.group_by{ |entry| entry.category }
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
index.html.erb
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<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>
<div class="col-md-4">
<p><%= link_to '@lala.join(" ")', :action %></p>
</div>
</div>
刮刀
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
参赛模式
class Entry < ApplicationRecord
validates :title, presence: true
validates :link, presence: true
validates :category, presence: true
end
路由
Rails.application.routes.draw do
#root 'entry#scrape_reddit'
root 'entries#index'
resources :entries
#get '/new_entries', to: 'entries#scrape', as: 'scrape'
end