- 首页
- 其他开发
- 使用 nokogiri 在源文档和每个项目之间创建关联
使用 nokogiri 在源文档和每个项目之间创建关联
[英] Creating an association between a source document and each item with nokogiri
本文介绍了使用 nokogiri 在源文档和每个项目之间创建关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建我的第一个 Web 应用程序,但有点迷茫.我已经阅读了 nokogiri 文档,并且知道我需要使用 Nokogiri::XML(open("http://foo"))
,但我不确定正确的位置在哪里使用它.
我有一个 series
模型,包含三列,:name
、:description
和 :url
.我想要它,所以当我创建一个新的 series
时,放置在 :url
表单字段中的 URL 被解析,并创建一个 episode
对于提要中的每个项目,使用 xpath 参数填充其列.
这个逻辑会出现在 series
模型或控制器中吗?还是在 episodes
模型或控制器中?
这是我现在拥有的.
系列
型号
类系列
系列
控制器
class SeriesController <应用控制器定义索引@series_all = Series.all结尾高清秀@series = Series.find(params[:id])结尾定义新@series = Series.new结尾定义编辑@series = Series.find(params[:id])结尾定义创建@series = Series.new(series_params)如果@series.save重定向到@series别的呈现新"结尾结尾销毁@series = Series.find(params[:id])@series.destroy重定向到 series_path结尾私人的def series_paramsparams.require(:series).permit(:name, :description, :url)结尾结尾
routes.rb
Rails.application.routes.draw 做资源:系列做资源:剧集结尾根'家#index'结尾
series
形式部分
<%= form_for @series do |f|%><div class="series-form"><div class="series-form__name"><%= f.label :name %><%= f.text_field :name %>
<div class="series-form__description"><%= f.label :description %><%= f.text_area :描述%>
<div class="series-form__url"><%= f.label :url %><%= f.text_field :url %>
<div class="series-form__submit"><%= f.submit %>
<%结束%>
episodes
有一个没有模型的空控制器.
<小时>
澄清一下,我正在尝试导入播客.因此,以 The Stack Exchange Podcast 的 RSS feed 为例,我会将其放在 series
形式:
名称:Stack Exchange 播客(来自 channel/title
)
说明:由 Joel Spolsky 与 Jay Hanlon 和 David Fullerton 共同主持...(来自 channel/itunes:summary
)
网址:https://blog.stackoverflow.com/feed/podcast/
并且它会为提要中的每个 item
创建一个 episode
.如果我能自动抓取 channel/title
和 channel/itunes:summary
就更好了.我希望这是可能的并且有意义.
解决方案
您实际上并不需要 Nokogiri.只需使用 Ruby 附带的 RSS 模块标准库.
至于您的问题,您似乎想用来自 RSS 提要的信息填充 ActiveRecord
模型.最简单的方法,尤其是当您不打算对应用程序中的任何其他数据执行此操作时,是让控制器调用 RSS 并将数据映射到 Series
和 Episode
模型那里.如果你这样做,它会更干净(也更容易测试)创建可以获取 RSS 数据并从频道数据生成 Series
和从项目数据生成 Episodes
的特殊对象.
下一步是将 RSS 视为另一种形式的数据存储(很像 SQLite 或 MySQL 数据库),并创建一个可以与 RSS 端点交互并生成映射模型的代码层(例如 RSSSeries
和 RSSEpisodes
在您的情况下)通过类似于 ActiveRecord
的界面(但当然是只读的).然后控制器可用于获取 RSSSeries
并访问相关的 RSSEpisodes
将其转换为 ActiveRecord
Series
和 >Episodes
建模并保存它们.
I'm building my first web application and am sort of lost. I've read the nokogiri docs, and know that I need to use Nokogiri::XML(open("http://foo"))
, but I'm not sure where the proper place is to use it.
I have a series
model with three columns, :name
, :description
and :url
. I want to have it so when I create a new series
, the URL placed in the :url
form field is parsed, and creates an episode
for each item in the feed, populating its columns with xpath arguments.
Would this logic go in the series
model or controller? Or in the episodes
model or controller?
Here's what I have now.
series
model
class Series < ActiveRecord::Base
validates :name, presence: true
validates :description, presence: true
validates :url, presence: true
has_many :episodes, dependent: :destroy
end
series
controller
class SeriesController < ApplicationController
def index
@series_all = Series.all
end
def show
@series = Series.find(params[:id])
end
def new
@series = Series.new
end
def edit
@series = Series.find(params[:id])
end
def create
@series = Series.new(series_params)
if @series.save
redirect_to @series
else
render 'new'
end
end
def destroy
@series = Series.find(params[:id])
@series.destroy
redirect_to series_path
end
private
def series_params
params.require(:series).permit(:name, :description, :url)
end
end
routes.rb
Rails.application.routes.draw do
resources :series do
resources :episodes
end
root 'home#index'
end
series
form partial
<%= form_for @series do |f| %>
<div class="series-form">
<div class="series-form__name">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="series-form__description">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="series-form__url">
<%= f.label :url %>
<%= f.text_field :url %>
</div>
<div class="series-form__submit">
<%= f.submit %>
</div>
</div>
<% end %>
episodes
has an empty controller with no model yet.
Edit: To clarify, I'm trying to import podcasts. So using The Stack Exchange Podcast's RSS feed as an example, I would place this in the series
form:
Name: The Stack Exchange Podcast (from channel/title
)
Description: Hosted by Joel Spolsky with Jay Hanlon and David Fullerton . . . (from channel/itunes:summary
)
URL: https://blog.stackoverflow.com/feed/podcast/
and it would create an episode
for each item
in the feed. It would be even better if I could grab channel/title
and channel/itunes:summary
automatically. I hope this is possible and makes sense.
解决方案
You don't really need Nokogiri for this. Just use the RSS module that comes with Ruby's standard lib.
As to your question, it looks like you want to populate the ActiveRecord
model with the information from RSS feed. The simplest way, especially if you are not planning to do this for any other data in your application, is to have controller make the call to RSS and map the data to the Series
and Episode
model there. It would be cleaner (and easier to test) if you
create special objects that can take the RSS data and generate Series
from the channel data and Episodes
from the item data.
The next step would be to treat RSS as another form of data storage (much like SQLite or MySQL databases), and create a layer of code that can interact with an RSS end-point and produce a mapped model (say RSSSeries
and RSSEpisodes
in your case) through an interface similar to ActiveRecord
(but read-only, of course). Controller then can be used to fetch the RSSSeries
and access related RSSEpisodes
translate it to the ActiveRecord
Series
and Episodes
models and save them.
这篇关于使用 nokogiri 在源文档和每个项目之间创建关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!