将控制权从一个控制器传递给另一个控制器
Pass control from one controller to other controller
我想通过我的 rails 应用程序从 RSS 中获取文章。我有一个网页,其中有一个按钮可以执行这些文章的抓取。有一个用户和文章模型,我已经定义了用于抓取这些文章的控制器。这些抓取控制器应该从 RSS 提要中获取值并将值传递给文章控制器。
我是 rails 的新手,不确定如何执行此操作。以下是我到目前为止所做的。
class ArticlesController < ApplicationController
def create
@article = Article.new(article_params)
@article.user = current_user
respond_to do |format|
if @article.save
format.html { redirect_to rss_importer_path }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:author, :title, :summary, :url, :date, :image, :user_id)
end
end
class RssImportersController < ApplicationController
def scrape
url = 'http://tviview.abc.net.au/rss/category/abc1.xml'
open(url) do |rss|
feed = RSS::Parser.parse(rss)
feed.items.each do |item|
article_path(author => nil, title => item.title, summary => item.description,
source => item.link, date => item.pubDate, image => nil)
end
end
end
end
在routes.rb-
resources :rss_importers
这是我的看法-
<div class="btn-group pull-left" role="group">
<%= link_to 'Scrape Articles', rss_importer_path ,class: "btn btn-default" %>
</div>
<br> <br>
<h1>All Articles</h1>
<% @articles.each do |article| %>
<%= render partial: 'index_article', locals: {article: article} %>
<% end %>
我建议不要使用控制器进行抓取。您可以创建一个服务 class 并从文章控制器调用它。
在app/services/rss_importer.rb:
class ImportRss
def call(params)
#Import code here
end
end
然后在你的文章控制器中:
def create
result = ImportRss.new.call(params)
#Rest of your codes
end
这是一篇关于如何使用服务 class 的好博客 post:http://adamniedzielski.github.io/blog/2014/11/25/my-take-on-services-in-rails/
我想通过我的 rails 应用程序从 RSS 中获取文章。我有一个网页,其中有一个按钮可以执行这些文章的抓取。有一个用户和文章模型,我已经定义了用于抓取这些文章的控制器。这些抓取控制器应该从 RSS 提要中获取值并将值传递给文章控制器。
我是 rails 的新手,不确定如何执行此操作。以下是我到目前为止所做的。
class ArticlesController < ApplicationController
def create
@article = Article.new(article_params)
@article.user = current_user
respond_to do |format|
if @article.save
format.html { redirect_to rss_importer_path }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:author, :title, :summary, :url, :date, :image, :user_id)
end
end
class RssImportersController < ApplicationController
def scrape
url = 'http://tviview.abc.net.au/rss/category/abc1.xml'
open(url) do |rss|
feed = RSS::Parser.parse(rss)
feed.items.each do |item|
article_path(author => nil, title => item.title, summary => item.description,
source => item.link, date => item.pubDate, image => nil)
end
end
end
end
在routes.rb-
resources :rss_importers
这是我的看法-
<div class="btn-group pull-left" role="group">
<%= link_to 'Scrape Articles', rss_importer_path ,class: "btn btn-default" %>
</div>
<br> <br>
<h1>All Articles</h1>
<% @articles.each do |article| %>
<%= render partial: 'index_article', locals: {article: article} %>
<% end %>
我建议不要使用控制器进行抓取。您可以创建一个服务 class 并从文章控制器调用它。
在app/services/rss_importer.rb:
class ImportRss
def call(params)
#Import code here
end
end
然后在你的文章控制器中:
def create
result = ImportRss.new.call(params)
#Rest of your codes
end
这是一篇关于如何使用服务 class 的好博客 post:http://adamniedzielski.github.io/blog/2014/11/25/my-take-on-services-in-rails/