2012-10-17 77 views
0

在我的數據庫中有一列名爲「startDate」。存儲的startDate的示例是2000-01-01 12:01:01.000000。 (此日期與Created_at或modified_at日期不同,它將是此視圖的show.html.erb中提及的事件發生的日期。)文本鏈接到搜索參數在Rails中傳遞的搜索參數

在文章的顯示視圖中,我想鏈接到顯示類似微博的結果(索引)頁面。我不知道如何做到這一點。

與鏈接的文本可以這樣說:

There are 17 events within 10 days before or after the date Wednesday October 17th, 2009.

詞語「17個事件」將是一個結果頁面的鏈接。在這個特殊的例子中,搜索的範圍將是20天10天總前週三2009年10月17日,並後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 

這是數據庫列的屏幕截圖...

DB Screenshot

回答

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 

我可能有錯誤的時間功能,但check here for details

現在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 

一種雜亂無章的答案,但我認爲它會在主點:-)

+0

我想我可能已經在我的文章用的是「文章」甩你。我的文章中的所有內容都與名爲「microposts」的數據庫相關。我試了一下你的代碼,用「micropost」代替了「article」這個詞。我假設你的第一塊代碼進入** microposts_controller.rb **,第二個進入** show.html.erb **我已經更新了我的原始帖子,我試過了。最後,我收到錯誤消息:nil:NilClass的未定義方法'count'。哦,我的名字是「startDate」。我是新手。 – Livi17

+0

新手歡迎。我也是,從2007年開始一直在做Rails。但是我發佈的任何代碼都只是你需要做的一個總的要點。第一個塊「def related_articles ...」放在你的'micropost.rb'模型中。第二個(「有<%= link_to ...」)進入視圖('show.html.erb')。最後進入控制器。我的建議是將問題分解成大塊。查看development.log文件,以查看您訪問網站上的任何頁面時獲得的內容 - 特別是路徑和參數數組。 –

+0

再次感謝您的幫助。我希望這隨着時間的推移變得更容易我再次嘗試了你的解決方案,但是在show.html.erb中,我得到了以下錯誤:'nil:NilClass'的undefined方法'count'在這一行上:**

<%= link_to(pluralize(「事件「,@ microposts.count),」#{microposts_path}?related =#{@micropost.id}「)%> 10天內...

**。在我的microposts_controller.rb中是否有缺失? – Livi17