2012-12-20 28 views
1

我對Rails相當陌生,我嘗試使用codecanyon中的jQuery newsticker來顯示已輸入數據庫的最新事件標題。例如這裏: http://codecanyon.net/item/jnewsticker-jquery-news-ticker/full_screen_preview/2137525Rails 3中的jQuery Newsticker

眼下,它顯示數據庫中的每個條目,並在事件表也都行,而不僅僅是標題,我覺得劇本是對窒息。

我希望它只顯示10個最近的事件。

在我events_helper.rb幫助我:

module EventsHelper 

    def populate_event 
     @events.each do |event| 
     content_tag(:li, link_to(event.title, '#')) 
     end 
    end 

end 

在我events_controller.rb控制器我有:

class EventsController < ApplicationController 
    before_filter :signed_in_user 

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

    def destroy 
    end 

    def show 
    @event = Event.find(params[:id]) 
    @events = Event.recent 
    end 
end 

在我event.rb模型我有:

scope :recent, order(updated_at: 'DESC') 

在我_ticker.html.erb部分我有

<ul id="newsticker_1" class="newsticker"> 
    <%= populate_event %> 
</ul> 

當我看到在瀏覽器的源代碼,有列表中沒有<li>標籤。

它看起來像這樣:

<ul id="newsticker_1" class="newsticker" style="position: absolute; left: 10px;"> 
    [#&lt;Event id: 29196, title: "This is a title", tag: nil, privacy_level: 1, group: nil, image_url: nil, start_date: nil, end_date: nil, start_location: nil, end_location: nil, start_geolocation: nil, end_geolocation: nil, content: "Quia officiis voluptatum doloribus cum ut ea sed ve...", user_id: 2, created_at: "2012-12-09 03:51:26", updated_at: "2012-12-09 03:51:26"&gt;, #&lt;Event id: 29190, title: "This is a title", tag: nil, privacy_level: 1, group: nil, image_url: nil, start_date: nil, end_date: nil, start_location: nil, end_location: nil, start_geolocation: nil, end_geolocation: nil, content: "Dolor consequatur sed enim omnis asperiores fugit r...", user_id: 2, created_at: "2012-12-09 03:51:26", updated_at: "2012-12-09 03:51:26"&gt; 
</ul> 

它應該是這個樣子:

<ul id="newsticker_1" class="newsticker"> 
    <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</li> 
    <li>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip...</li> 
    <li>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum...</li> 
    <li>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia...</li> 
    <li>Bubble bubble ipsum dolor sit amet, consectetur adipisicing elit...</ li> 
</ul> 

UPDATE:

繼Dimuch以下

這是建議代理商看起來像什麼?瀏覽器: enter image description here

這是HTML源代碼是幹什麼:

enter image description here

回答

1

你誤會助手使用。它不會將內容輸出到生成的頁面,它會返回插入頁面的值。

所以你的幫助方法返回事件的集合。嘗試將mapjoin結合使用。

def populate_event 
    @events.map do |event| 
    content_tag(:li, link_to(event.title, '#')) 
    end.join 
end 
+0

謝謝您的幫助。您的答案可以限制顯示的內容僅限於標題。有沒有一種方法可以將此限制爲最近剛剛輸入的10個事件? – Livi17

+0

範圍:最近,訂單(updated_at:'DESC')。limit(10)'在模型中或'@events = Event.recent。限制(10)'在控制器應該工作。 – dimuch

+0

我不認爲.limit(10)正在工作,但我不認爲這是你的代碼是問題所在。我在瀏覽器中顯示的是所有的HTML標籤,它看起來像是一個很長的字符串。有30個迭代:'

  • This is a title
  • '我會用截圖更新我的問題。 – Livi17

    1

    以下dimuch的答案來解決輔助方法後,調用該方法html_safe:

    <ul id="newsticker_1" class="newsticker"> 
        <%= populate_event.html_safe %> 
    </ul>