简体   繁体   English

发布文章时如何创建到DATE的链接,并列出发布在同一DATE的所有文章

[英]How to create a link to the DATE when ARTICLES were posted, and list all the articles posted on the same DATE

I need to be able to list the month, day and year in which each one of my articles were created. 我需要列出创建每篇文章的月份,日期和年份。 Until this point everything works fine. 到目前为止,一切正常。

I need to GROUP my articles according to the DAY they were created. 我需要根据文章的创建日期对文章进行分组。 For example: 例如:


  • October 4th, 2017 ( this must be a link that, once cliked, leads me to a page with a collection of ALL the articles created on that day ). 2017年10月4日 (这必须是一个链接,一旦被点赞,便会带我到一个页面,其中包含当天创建的所有文章的集合)。

      Besides making "October 4th, 2017" a link to all the articles, I also need to list those same articles titles below "October 4th, 2017" on the initial page. 
    • First Article ( link to that individual article ) 第一篇文章 (链接到该文章)
    • Second Article ( link to that individual article ) 第二条 (指向该条的链接)
    • Third Article ( link to that individual article ) 第三条 (指向该条的链接)
    • And so on ... 等等 ...

My code looks hacky, but until now everything works EXCEPT for the fact that the "October 4th, 2017" link doesn't lead me to a page where I can see all the articles, but it gives me a weird URL that only shows the very first article. 我的代码看上去很hacky,但是直到现在为止,所有内容都可以正常运行,原因是“ 2017年10月4日”链接不会将我引向可以看到所有文章的页面,但是它给了我一个奇怪的URL,仅显示了第一篇文章。 It looks like this: 看起来像这样:


website.com/articles.34%2F38%2F39%2F40%2F41%2F42 website.com/articles.34%2F38%2F39%2F40%2F41%2F42

Here's my code: 这是我的代码:

  articles_controller.rb

  def listarticles

    @articles = Article.order(date: :desc) 
    @article_days = @articles.group_by { |t| t.date.beginning_of_day }

  end

listarticles.html.erb

<% @article_days.each do |day, articles| %>

   <%= link_to day.strftime('%d''%B' '%Y'), articles_path(articles) %>

   <% articles.each do |article| %>

       <%= link_to article.title, article_path(article) %>

   <% end %>
<% end %>

Thank you for helping! 感谢您的帮助!

The offending line is this: 令人讨厌的行是这样的:

articles_path(articles)

You've passed an array of articles to the path, which Rails converts to their ids. 您已将一系列文章传递给路径,Rails会将其转换为它们的ID。 So this translates to something like: 因此,这相当于:

/articles.34/38/39/40/41/42

This gets encoded into what you get there ( "/" replaced by %2F) 这将被编码为您所能到达的位置(“ /”替换为%2F)

I'm not sure how you got the first article from this. 我不确定您如何从中获得第一篇文章。 But usually the part after the dot denotes the format, as you can see if you rake your routes: 但通常点后的部分表示格式,如您所见,如果您倾斜路线,则可以看到:

articles  GET  /articles(.:format)   articles#index

In order for the #index to only show the list you want, you need to send it as a query string: 为了使#index仅显示所需的列表,您需要将其作为查询字符串发送:

articles_path(ids: articles.map(&:id).join(","))

This will give you 这会给你

/articles?ids=34,38,39,40,41,42

Then in your controller, you need something like this: 然后在您的控制器中,您需要这样的东西:

def index
  ids = params[:ids].split(",") if params[:ids]
  @articles = Article.where(id: ids) if ids
  @articles ||= Article.all
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM