この質問の基本的な前提は、タグのリンクを動作させる方法です。クリックすると、関連するすべてのエントリが表示されます。タグをエントリに関連付けるのに問題がある
しかし、リンクをクリックすると、基本的にタイトルにもかかわらず、空白のページが表示されます。 は、そのタグに関連付けられたエントリをに示していません。
アソシエーションはどこかで動作していませんか?または、タグコントローラの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
エントリーモデル
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
<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>
index.html.erbエントリ
タグ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 =鋸山:: 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