2011-08-21 105 views
0

我很抱歉我的英语不好。我只是试图描述我的问题。 :) 我有一个应用程序布局,有一个产量显示职位在体内。我有另一个yield:footerpost3用于在页脚上显示最近帖子的标题。Ruby on Rails 3:玩意

当我在localhost:3000时,yield:footerpost3正确显示最近的标题。但是当我点击一个帖子链接,这是url是localhost:3000/posts/3,yield:footerpost3什么都不显示。

这里是我的代码: 应用程序/视图/布局/ application.html.erb

<!-- begin footer comment widget --> 
    <div class="footer_list widget_recent_comments"> 
    <div class="title"><h5>Artikel Terkini</h5></div> 
     <%= yield :footerpost3 %>    
    </div> 
<!-- end footer comment widget --> 

应用程序/视图/存储/ index.html.erb

<% content_for :footerpost3 do %> 
    <% @postsMain.each do |dopostAll| %> 
     <div class="entry"> 
      <ul> 
       <li class="recentcomments"><%= link_to dopostAll.title, dopostAll %></li> 
      </ul> 
     </div>       
    <% end %> 
<% end %> 

我希望我的问题很容易理解.. :)

回答

0

看起来像你的根网址是stores#index。 您必须在stores#index操作中初始化@postsMain并在stores/index.html.erb中生成content_for footerpost3

当你点击一个帖子,你将被带到posts#show page。所以,你必须即使在posts#show行动初始化@postsMain和生成,即使在posts/show.html.erb

0

答案就在你的问题。您正在定义该块中的“footerpost3的内容”,它存在于index.html.erb中。当你在/posts/3时,index.html.erb没有被渲染,而是show.html.erb是。

要解决这个问题,您还需要在show.html.erb模板中添加内容。

您可以通过多种方式解决此问题。使用嵌套布局将是一个。例如,你可能app/views/layout/posts.html.erb创建一个帖子布局,像这样:

<% content_for :footerpost3 do %> 
    <% @postsMain.each do |dopostAll| %> 
     <div class="entry"> 
      <ul> 
       <li class="recentcomments"><%= link_to dopostAll.title, dopostAll %></li> 
      </ul> 
     </div>       
    <% end %> 
<% end %> 

<%= render :file => 'layouts/application' %> 

这样一来,你的PostsController将使用此布局,这只是增加了您的页脚内容的所有意见,然后才呈现application_layout。

+0

footerpost3的内容,所以我必须插入一个 <%=渲染:文件=>在每一个“布局/应用程序”%> 代码中的另一个看法? 有没有简单的方法?像应用程序帮手?老实说,我不明白应用帮手:) – mamatozay

+0

不是每个视图。这个例子显示了你如何使用布局来做到这一点。您的所有帖子视图(索引,显示,新建,编辑)将自动在此布局中呈现。所以你只能把它放在一个地方。诀窍是你必须确保所有在这个布局内渲染的视图都定义了@ postsMain,否则它们会被破坏。 – numbers1311407

+0

感谢您的帮助。 我知道明白了。 – mamatozay