Rails Feed解析模型和参数

时间:2017-03-30 08:51:17

标签: ruby-on-rails ruby rss

StackOverflow的女神和半神,

我一直试图弄清楚这个小问题1周,3天,17小时,19分钟和59个土豆。

我是Rails的新手,我正在尝试创建一个可以接收多个RSS源的应用程序。我需要每个Feed都属于它来自的相应新闻点。如何填充Feed Entries数据库?

这就是我所要做的......

FeedEntry模型

class FeedEntry < ApplicationRecord
    belongs_to :news_outlet
    def self.update_from_feed(feed_url)
        feed = Feedjira::Feed.fetch_and_parse(feed_url)
        feed.entries.each do |entry|
            unless exists? :guid => entry.id
                create!(
                    :name           => entry.title,
                    :summary        => entry.summary,
                    :url            => entry.url,
                    :published_at   => entry.published,
                    :guid           => entry.id

                )
            end
        end
    end
end

NewsOutlet模型

 class NewsOutlet < ApplicationRecord
    has_many :feed_entries
end

NewsOutlet控制器

class NewsOutletsController < ApplicationController
    before_action :authenticate_user!, except: [:index, :show]
  before_filter :configure_permitted_parameters, if: :devise_controller?
  before_filter :verify_is_admin, except: [:index, :show]

  def index
    @news_outlets = NewsOutlet.all
  end

  def new
    @news_outlet = NewsOutlet.new
  end

  def create
    @news_outlet = NewsOutlet.new(news_outlet_params)

    @news_outlet.save
    redirect_to @news_outlet
  end

  def show
    @news_outlet = NewsOutlet.find(params[:id])
  end

  def edit
    @news_outlet = NewsOutlet.find(params[:id])
  end

  def update
    @news_outlet = NewsOutlet.find(params[:id])
    if @news_outlet.update news_outlet_params
      redirect_to @news_outlet
    else
      render 'edit'
    end
  end

  def delete
  end

  def destroy
    @news_outlet = NewsOutlet.find(params[:id])
    @news_outlet.destroy
    redirect_to news_outlets_path
  end  

  private

  def news_outlet_params
    params.require(:news_outlet).permit(:name, :url)
  end
end

我的NewsOutlet#Index以防万一,我想请求FeedEntry DB用新闻标题填充它。

<h1>Blah</h1>   
 <% if @news_outlets.present? %>
        <% @news_outlets.each do |news_outlet| %>
            <% entries = FeedEntry.update_from_feed(news_outlet.url) %>
            <p><%= link_to "#{news_outlet.name}", news_outlet.url %></p>

            <% entries.first(5).each do |entry| %>
                <%= link_to "#{entry.title}", entry.url %><br>
            <% end %>
            <p><%= link_to "Show", news_outlet_path(news_outlet) %></p>
            <hr>
        <% end %>
    <% else %>
        <p>No news outlets yet...</p>
    <% end %>

祝你在大灾难中有许多土豆。任何帮助都非常感谢&lt; 3

1 个答案:

答案 0 :(得分:0)

我自己没有测试过,但我认为这应该可行:

NewsOutlet模型

class NewsOutlet < ApplicationRecord
  has_many :feed_entries

  def update_from_feed
    entries = Feedjira::Feed.fetch_and_parse(self.url)

    entries.each do |entry|
      feed_entries.where(guid: entry.id).first_or_create! do |fe|
        fe.name = entry.title
        fe.summary = entry.summary
        fe.url = entry.url
        fe.published_at = entry.published
      end
    end

    entries.first(5)
  end

end

FeedEntry模型

class FeedEntry < ApplicationRecord
  belongs_to :news_outlet
end

查看

<% @news_outlets.each do |news_outlet| %>
  <p><%= link_to "#{news_outlet.name}", news_outlet.url %></p>
  <% news_outlet.update_from_feed.each do |entry| %>
    <%= link_to "#{entry.title}", entry.url %><br>
  <% end %>
  <p><%= link_to "Show", news_outlet_path(news_outlet) %></p>
  <hr>
<% end %>