开发者

Ruby on Rails 3 : Playing with views

I am sorry for my bad english. I just try to description my question. :) I have an application layout that have a yield for display post in body. I have another yield :footerpost3 for display title of recent post on the footer.

When I in localhost:3000, the yield :footerpost3 display a recent of title correctly. but when i am click a post link, which is the url is localhost:3000/posts/3, the yield :footerpost3 display nothing.

Here is my code: app/views/layout/application.html.erb

<!-- begin footer comment widget -->
  <div class="footer_list widget_recent_comments">
    <div class="title"><h5>Artikel Terkini</h5></div>
      <%= yiel开发者_运维技巧d :footerpost3 %>             
    </div>
<!-- end footer comment widget -->

app/views/store/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 %>

i hope my question is easy to understand.. :)


Looks like your root url is stores#index . You must be initializing @postsMain in the stores#index action and generating the content_for footerpost3 in stores/index.html.erb.

When you click on a post, you will be taken to posts#show page. So you have to initialize @postsMain even in posts#show action and generate the content for footerpost3 even in posts/show.html.erb


The answer is there in your question. You are defining the "content for" footerpost3 in that block, which exists in index.html.erb. When you're on /posts/3, index.html.erb is not rendered, but rather show.html.erb is.

To solve this, you'd need to add the content in the show.html.erb template as well.

You could solve this in multiple ways. Using nested layouts would be one. For example, you might create a posts layout at app/views/layout/posts.html.erb, like so:

<% 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' %>

In this way, all the views of your PostsController would use this layout, which simply adds your footer content, then renders the application_layout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜