在我的数据库中有一个名为“ 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"> </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
这是数据库列的屏幕截图...