有问题的标签控制器

时间:2017-03-19 20:08:15

标签: ruby-on-rails ruby

我有两个三个模型,其中两个是:through association

基本上,我要做的是在与特定标签关联时显示所有条目。

条目本身也会显示在索引页面上,标签也是如此。单击标签时,它基本上显示一个空白页面。

最初,我认为问题是标签与条目没有关联并且有问题here。我已被告知,并创建一个新问题。

请帮助我了解这里发生了什么,这样我就可以解决这个问题并让它发挥作用。

谢谢。

报废数据

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']
      name = entry.css('p.tagline > a.subreddit')[0]['href']
      Entry.create!(title: title, link: link)
      Tag.create!(name: name)

    end
  end

end

标记模型

class Tagging < ApplicationRecord
  belongs_to :tag
  belongs_to :entry
end

标记模型

class Tag < ApplicationRecord
    has_many :taggings
    has_many :entries, through: :taggings

    validates :name, presence: true
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 EntriesController < ApplicationController

    def index 
        @entries = Entry.all 
        @tags = Tag.all
    end

  def show
    @entry = Entry.find(params[:id])
  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

标签控制器(第1部分 - 我认为问题所在的位置)

class TagsController < ApplicationController

    def index
        @tags = Tag.all 
    end

    def show 
        @tag = Tag.find(params[:id])
    end
end

这是参赛作品&#39; 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">
  <%= @tags.class %>
    <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 %>

1 个答案:

答案 0 :(得分:0)

在你的刮刀方法中你有这个:

  Entry.create!(title: title, link: link)
  Tag.create!(name: name)

请记住,通过关联设置,条目仅链接到标记标记。你不是在创作。

这样做:

  entry = Entry.create!(title: title, link: link)
  tag = Tag.create!(name: name)
  Tagging.create(entry_id: entry.id, tag_id: tag.id)
相关问题