這個問題的基本前提是如何使標籤鏈接工作,所以當你點擊它時,它會出現與此相關的所有條目。將標籤與條目關聯的問題
但是,當您點擊鏈接時,基本上,儘管它的標題,空白頁顯示出來。它不顯示與其標籤相關聯的條目。
那麼顯然協會不在某個地方工作?或標籤控制器中的show
方法不正確?不確定到底要修復什麼。
這是到目前爲止我的代碼(即屬於空白頁的部分是在最底層)
條目控制器
class EntriesController < ApplicationController
def index
@entries = Entry.all
@tags = Tag.all
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
標籤控制器
class TagsController < ApplicationController
def index
@tags = Tag.all
end
def show
@tag = Tag.find(params[:id])
end
end
Entry Model
class Entry < ApplicationRecord
has_many :taggings
has_many :tags, through: :taggings
validates :title, presence: true
validates :link, presence: true
def tag_list
tags.join(", ")
end
def tag_list=(tags_string)
tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name) }
self.tags = new_or_found_tags
end
end
標籤型號
class Tag < ApplicationRecord
has_many :taggings
has_many :entries, through: :taggings
validates :name, presence: true
end
標註模型
class Tagging < ApplicationRecord
belongs_to :tag
belongs_to :entry
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>
</div>
</div>
<% end %>
</div>
</div>
<div class="col-md-4">
<p>Tags: <% @tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</div>
</div>
標籤show.html.erb(這是這部分不工作 - 即與標籤相關的條目 - 頁面顯示爲空白)
<h1>Entries Tagged with <%= @tag.name %></h1>
<ul>
<% @tag.entries.each do |entry| %>
<li><%= link_to entry.title, entry_path(entry) %></li>
<% end %>
</ul>
<%= link_to "All Tags", tags_path %>
Rails的控制檯
e = Entry.first
Entry Load (28.8ms) SELECT "entries".* FROM "entries" ORDER BY "entries"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Entry id: 4, title: "Sweet old man at the beach: \"Would you like me to ...", link: "/r/funny/comments/5zut3e/sweet_old_man_at_the_beac...", created_at: "2017-03-17 08:20:25", updated_at: "2017-03-17 08:20:25">
irb(main):036:0> t = Tag.first
Tag Load (0.5ms) SELECT "tags".* FROM "tags" ORDER BY "tags"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Tag id: 3, name: "https://www.reddit.com/r/funny/", created_at: "2017-03-17 08:20:26", updated_at: "2017-03-17 08:20:26">
irb(main):037:0> tag.entries
NoMethodError: undefined method `entries' for nil:NilClass
的Rails控制檯2
irb(main):041:0> tag = Tag.first
Tag Load (2.6ms) SELECT "tags".* FROM "tags" ORDER BY "tags"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Tag id: 3, name: "https://www.reddit.com/r/funny/", created_at: "2017-03-17 08:20:26", updated_at: "2017-03-17 08:20:26">
irb(main):042:0> tag.entries << Entry.first
Entry Load (0.8ms) SELECT "entries".* FROM "entries" ORDER BY "entries"."id" ASC LIMIT $1 [["LIMIT", 1]]
(32.6ms) SAVEPOINT active_record_1
SQL (318.8ms) INSERT INTO "taggings" ("tag_id", "entry_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["tag_id", 3], ["entry_id", 4], ["created_at", 2017-03-18 05:05:24 UTC], ["updated_at", 2017-03-18 05:05:24 UTC]]
(0.3ms) RELEASE SAVEPOINT active_record_1
Entry Load (1.3ms) SELECT "entries".* FROM "entries" INNER JOIN "taggings" ON "entries"."id" = "taggings"."entry_id" WHERE "taggings"."tag_id" = $1 [["tag_id", 3]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Entry id: 4, title: "Sweet old man at the beach: \"Would you like me to ...", link: "/r/funny/comments/5zut3e/sweet_old_man_at_the_beac...", created_at: "2017-03-17 08:20:25", updated_at: "2017-03-17 08:20:25">]>
irb(main):043:0> tag.save
(0.4ms) SAVEPOINT active_record_1
(0.3ms) RELEASE SAVEPOINT active_record_1
=> true
irb(main):044:0> Tag.first.entries
Tag Load (0.8ms) SELECT "tags".* FROM "tags" ORDER BY "tags"."id" ASC LIMIT $1 [["LIMIT", 1]]
Entry Load (1.1ms) SELECT "entries".* FROM "entries" INNER JOIN "taggings" ON "entries"."id" = "taggings"."entry_id" WHERE "taggings"."tag_id" = $1 [["tag_id", 3]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Entry id: 4, title: "Sweet old man at the beach: \"Would you like me to ...", link: "/r/funny/comments/5zut3e/sweet_old_man_at_the_beac...", created_at: "2017-03-17 08:20:25", updated_at: "2017-03-17 08:20:25">]>
irb(main):045:0>
個
導軌刮碼
需要 '開URI'
模塊RedditScrapper
高清自我。刮 DOC =引入nokogiri :: HTML(開放( 「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']
name = entry.css('p.tagline > a.subreddit')[0]['href']
Entry.create!(title: title, link: link)
Tag.create!(name: name)
end
末
末
有什麼問題嗎?你在跑什麼? 「不知道如何使用X」對於StackOverflow來說並不是一個很好的問題,不需要狹窄的範圍。 – coreyward
@coreyward,是的,我知道。對不起,有一點漫步。讓我澄清一下問題的最上面部分...讓我知道是否需要進一步調整。 – user273072545345
@coreyward,嘿,請讓我知道這是否更好?謝謝。快樂聖帕特里克日btw! – user273072545345