0

在我的数据库中有一个名为“ startDate ”的列。存储的 startDate 示例是2000-01-01 12:01:01.000000。(此日期不同于 Created_at 或 modified_at 日期。它将是此视图的 show.html.erb 中提到的事件发生的日期。)

在一篇文章的显示视图中,我想链接到一个结果(索引)页面,其中将显示类似的微博。我不知道该怎么做。

带有链接的文本可能会这样说:

在 2009 年 10 月 17 日星期三之前或之后的 10 天内有 17 场活动。

单词“ 17 events ”将是结果页面的链接。此特定示例中的搜索跨度总共为20 天,其中 2009年10 月 17 日星期三之前的 10天和之后的 10 天。

这是我当前的 show.html.erb 页面。

<div class="row">
  <div class="span2"><center><img src=<%= @micropost.imageURL %> class="img-polaroid"></center></div>

  <div class="span7">
        <h3><%= @micropost.title %></h3><br>
        <p><small><%= @micropost.loc1T %><br>
            <%= @micropost.startTime.strftime('%A, %B %d, %Y') %></small></p>
        <p><%= raw @micropost.content %></p>
    </div>

  <div class="span3"><img src="http://placehold.it/210x210" class="img-polaroid"><br>
    <small>advertisment</small>
    <div class="divider">&nbsp;</div>
    <div class="well">

    <p>There are <%= link_to(pluralize("event", @microposts.count), "#{microposts_path} related=#{@micropost.id}") %> within 10 days...</p>


  </div>

  </div>
</div>

模型/micropost.rb

class Micropost < ActiveRecord::Base
  attr_accessible :content, :title,:keyword,:privacy,:groups,:loc1T,:latitude,:longitude,:loc2T,:loc2Lat,:loc2Lon,:startTime,:endTime,:imageURL
  geocoded_by :loc1T
  after_validation :geocode#, :if => :address_changed?


  belongs_to :user

  validates :user_id, presence: true
  validates :title, presence: true
  validates :keyword, presence: true
  validates :privacy, presence: true
  validates :groups, presence: true
  validates :loc1T, presence: true
  validates :latitude, presence: true
  validates :longitude, presence: true
  validates :loc2T, presence: true
  validates :loc2Lat, presence: true
  validates :loc2Lon, presence: true
  validates :startTime, presence: true
  validates :endTime, presence: true
  validates :imageURL, presence: true

  validates :content, presence: true

  default_scope order: 'microposts.created_at DESC'

  def self.from_users_followed_by(user)
    followed_user_ids = "SELECT followed_id FROM relationships
                         WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids}) OR user_id = :user_id", 
          user_id: user.id)
  end

  def related_microposts(this_startTime)
  @microposts.where('startTime > ? AND startTime < ?', 5.days.since(startTime), 5.days.until(startTime))
  end
end

microposts_controller.rb

class MicropostsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  before_filter :correct_user,   only: :destroy

  def index
  end

   def show
    @micropost = Micropost.find(params[:id])
  end

  def related_microposts(this_startDdate)
  Micropost.where('startDate > ? AND startDate < ?', 5.days.since(this_startDate), 5.days.until(this_startDate))
  end

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    redirect_to root_url
  end

  private

    def correct_user
      @micropost = current_user.microposts.find_by_id(params[:id])
      redirect_to root_url if @micropost.nil?
    end
end

这是数据库列的屏幕截图...

数据库截图

4

1 回答 1

0

听起来有几个问题需要解决。

(按照 Rails 约定,我将假设您的专栏确实被称为start_date:-))

首先,要获取文章发布start_date后 5 天内的文章列表,您可以使用类似

def related_articles(this_start_date)
  Article.where('start_date > ? AND start_date < ?', 5.days.since(this_start_date), 5.days.until(this_start_date))
end

我的时间功能可能有误,但请查看此处了解详情

现在related_articles有你需要的。如果您只需要计数,您可以使用 获取它related_articles.count,或者保存到@articles. 在显示当前的主页中@article添加一个链接以显示相关文章,例如

There are <%= link_to(pluralize("event", @articles.count), "#{articles_path}?related=#{@article.id}") %> within 10 days...

articles_path是列出所有文章的视图的路径index,所以现在在控制器的index方法中添加一些代码,以便在存在特殊查询字符串参数时仅列出那些文章。

if params[:related]
  this_article = Article.find params[:related]  # 123 is the id of the main article
  @articles = Article.related_articles(this_article)
else
  @articles = Article.all
end

有点杂乱无章的答案,但我认为它很重要:-)

于 2012-10-18T03:08:59.443 回答