To output RSS in Rails 6, follow these steps:
I wrote it, but I got an error.
I don't know which step is wrong.
articles_controller.rb
defeed
@articles=Article.all.order(created_at::desc).page(params[:page]).per(6)
end
routes.rb
get 'articles/feed'
views/articles/feed.html.rss
<rss version='2.0'>
<channel>
<title> 1K Extreme: Updates </title>
<link>https://www.onekextreme.com/</link>
<description>1K Extreme Blog: New </description>
<%@articles.each do |article|%>
<item>
<title><%=article.title%></title>
<link>https://www.onekextreme.com/articles/#{article.id}</link>
<description><%=article.content1%></description>
<pubDate><%=article.created_at.strftime("%Y.%m.%d")%>/pubDate>
</item>
<%end%>
</channel>
</rss>
Error in local environment
http://localhost:3000/articles/feed
ArticlesController #feed is missing a template for request formats: text/html
NOTE!
Unlimited otherwise, Rails expect an action to render a template with the same name,
contained in a folder named after its controller.If this controller is an API responding with 204 (No Content),
which does not require a template, then this error will occur when trying to access it via browser,
since we expect an HTML template to be rendered for such requests. If that's the case, carry on.
Modify template file name
views/articles/feed.html.rss
=>views/articles/feed.rss.erb
modifying config/routes.rb
get 'articles/feed', defaults: {format::rss}
How about
© 2024 OneMinuteCode. All rights reserved.