2016-01-28 221 views
0

我在寫一個rails應用程序,我打算使用活動內容至少填寫側欄。具體來說,它意味着顯示應用程序用戶發送的最近消息。側邊欄的活動內容

現在,我只是試圖讓邊欄顯示最近的消息,而不管哪個控制器正在處理頁面。

視圖/佈局/ application.html.erb

<!DOCTYPE html> 
<html lang="en"> 
    <%= render 'layouts/head' %> 
    <body> 
    <%= render 'layouts/nav' %> 

    <div class="container"> 
     <div class="row"> 
     <%= render 'shared/sidebar_users' %> 

     <div class="col-lg-6"> 
      <%= bootstrap_flash %> 
      <%= yield %> 
     </div> 

     <%= render 'shared/sidebar_messages' %> 
     </div><!--/row--> 

     <%= render 'layouts/footer' %> 
    </div> <!-- /container --> 

    <%= render 'layouts/debug' %> 
    </body> 
</html> 

視圖/共享/ _sidebar_messages.html.erb

<div class="col-lg-3"> 
    <div class="well sidebar-nav"> 
    <h3>Recent Posts</h3> 

    <%= render partial: "messages/messages", locals: { display_actions: false } %> 
    </div><!--/.well --> 
</div><!--/span--> 

視圖/消息/ _messages.html.erb

<table> 
    <thead> 
    <tr> 
     <th>Creator</th> 
     <th>Content</th> 
     <% if @display_actions %> 
     <th colspan="3">Actions</th> 
     <% end %> 
    </tr> 
    </thead> 

    <tbody> 
    <% @messages.each do |message| %> 
     <tr> 
     <td><%= message.creator.full_name %></td> 
     <td><%= message.content %></td> 
     <% if display_actions %> 
      <td><%= link_to 'Show', message %></td> 
      <td><%= link_to 'Edit', edit_message_path(message) %></td> 
      <td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     <% end %> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

請注意代碼在此行中斷,指出@messages爲零,因此沒有每種方法。

<% @messages.each do |message| %> 
+0

讓我們知道下面的答案是否對你有用。 – SsouLlesS

+1

@SsouLlesS會做。 – Nuclearman

回答

1

那麼這裏發生了什麼是@messages沒有定義,當你與你需要的變量傳遞手動它諧音工作。你不能只在你的消息控制器中定義@messages實例變量,並期望在任何地方工作。

如果你的消息總是具有相同的邏輯,無論控制器,那麼你可以在你的應用程序控制器定義它,所以它會使用在你的視圖操作:

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    before_filter :set_messages 

    private 
    def set_var 
    @messages = #Logic here for get the messages 
    end 
end 

然後在任何視圖操作要渲染側邊欄,你需要通過變量:

<%= render 'shared/sidebar_messages', messages: @messages %> #Note that you do not need to use locals anymore in rails 4 

當然,在你的側邊欄,並通過messages_messages.html.erb

另一種方法是創建一個幫助並在局部用它來代替@messages變量:在您的應用助手

高清get_messages_for_sidebar #Here你可以得到你想要返回的消息。 結束

_messages.html.erb分打電話給你的助手

然後:

<table> 
    <thead> 
    <tr> 
     <th>Creator</th> 
     <th>Content</th> 
     <% if display_actions %> 
     <th colspan="3">Actions</th> 
     <% end %> 
    </tr> 
    </thead> 

    <tbody> 
    <% get_recent_messages.each do |message| %> 
     <tr> 
     <td><%= message.creator.full_name %></td> 
     <td><%= message.content %></td> 
     <% if display_actions %> 
      <td><%= link_to 'Show', message %></td> 
      <td><%= link_to 'Edit', edit_message_path(message) %></td> 
      <td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     <% end %> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

另一種方法是使用範圍,你message.rb模型中的適用範圍。

message.rb

scope :get_recent, -> { order(created_at: :desc).limit(10) } #get the last 10 most recent 

然後你可以叫你的部分Message.get_recent

+0

AJAX部分,這是另一個話題,請創建另一個問題並粘貼鏈接,我會回答你。 – SsouLlesS

2

創建的helper方法來獲得最近的消息,並呼籲在views/messages/_messages.html.erb

在你application_helper增加其返回最近的消息,例如

def get_recent_messages 
    #What ever the logic is to get recent messages i-e Message.order('created_at desc').limit(10) 
end 

然後在你的部分方法

<table> 
    <thead> 
    <tr> 
     <th>Creator</th> 
     <th>Content</th> 
     <% if display_actions %> 
     <th colspan="3">Actions</th> 
     <% end %> 
    </tr> 
    </thead> 

    <tbody> 
    <% (@messages || get_recent_messages).each do |message| %> 
     <tr> 
     <td><%= message.creator.full_name %></td> 
     <td><%= message.content %></td> 
     <% if display_actions %> 
      <td><%= link_to 'Show', message %></td> 
      <td><%= link_to 'Edit', edit_message_path(message) %></td> 
      <td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     <% end %> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

其次您可以創建一個名爲scopemessage.rb得到最新消息,然後調用你的部分像Message.recent