将标签与条目相关联的麻烦
这个问题的基本前提是如何使标签链接工作,所以当你点击它时,它会提供与之相关的所有条目。
但是,当您点击链接时,基本上,尽管有标题,但会出现一个空白页面。 它不显示与其标签关联的条目。
很清楚,这种关联在哪里不起作用? 或者标签控制器中的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
条目index.html.erb
Tags:
标签show.html.erb (这部分不起作用 – 即与标签相关的条目 – 页面显示为空白)
Entries Tagged with
Rails控制台
e = Entry.first Entry Load (28.8ms) SELECT "entries".* FROM "entries" ORDER BY "entries"."id" ASC LIMIT $1 [["LIMIT", 1]] => # irb(main):036:0> t = Tag.first Tag Load (0.5ms) SELECT "tags".* FROM "tags" ORDER BY "tags"."id" ASC LIMIT $1 [["LIMIT", 1]] => # 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]] => # irb(main):042:0> tag.entries < #<ActiveRecord::Associations::CollectionProxy [#]> 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 [#]> irb(main):045:0>
铁路刮板代码
要求’开放式’
模块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'] name = entry.css('p.tagline > a.subreddit')[0]['href'] Entry.create!(title: title, link: link) Tag.create!(name: name) end
结束
结束
更改您的scraper代码以构建关联。
代替…
Entry.create!(title: title, link: link) Tag.create!(name: name)
做…
entry = Entry.create!(title: title, link: link) tag = Tag.create!(name: name) tag.entries << entry tag.save!
Interesting Posts
Ruby on Rails有“phpmyadmin”吗?
参数超出范围Rails 4和bootstrap3-datetimepicker-rails
rails application helper不支持中文字符
Heroku文件上传问题
Rails belongs_to关联,在集合的一部分时无法访问所有者的属性?
如何解决超时问题(Ruby,Rails)
调用super()会导致父类中的其他方法被使用吗?
从数组和变量范围中提取索引
Ruby on Rails每次都会触发序列化属性的更新
编译没有rvm或rbenv的Ruby 2.0错误,`readline.c:1886:26:错误:’函数’未声明(在此函数中首次使用)