RSS Output Method for Rails 6

Asked 1 years ago, Updated 1 years ago, 339 views

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.

ruby-on-rails

2022-12-11 18:57

1 Answers

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


2022-12-12 08:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.