使用多个产量插入内容 [英] Using multiple yields to insert content

查看:93
本文介绍了使用多个产量插入内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用yield在页面上插入内容,但是每次操作都会从页面上删除整个内容.我有一个主要的yield,它运行正常:

I am trying to insert content on my page with yield but every time action removes whole content from the page. I have one main yield which is working fine:

<body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
</body>

但是在显示在一页上的新内容中,我还有另一个yield:

But inside that new content which is displayed on one page I have another yield:

<div class="container">
    <%= render 'admins/menu' %>
    <%= yield :admin %>
</div>

当用户单击呈现的菜单时,新内容应显示在该菜单下方.

When user clicks on the menu which is rendered, new content should be displayed below that menu.

admins/_menu.html.erb

admins/_menu.html.erb

<div class="navbar">  
    <div class="navbar-inner">  
        <div class="container">  
            <ul class="nav">
                <li><%= link_to "Users", :controller => "admins", :action => "test" %></li>
                <li><%= link_to "1", ... %></li>
                <li><%= link_to "2", ... %></li>
                <li><%= link_to "3", ... %></li>
            </ul>
        </div>  
    </div>  
</div>  

控制器:

class AdminsController < ApplicationController

    def index
    end

    def test
        @users = User.paginate(page: params[:page])
    end
end

test.html.erb

test.html.erb

<% content_for :admin do %>

<h1>All users</h1>

...

<% end %>

当我从菜单中单击用户"选项时,页面刷新,菜单消失,并且在正文"中没有任何显示.我希望内容显示在菜单下方.如何使用第二个产量并完成此功能?

When I click on the option 'Users' from menu, page refreshes, menu disappears and nothing is displayed inside `body'. I want the content to be displayed below menu. How to use that second yield and accomplish this functionality?

我希望这个问题不会引起混淆.如果问题令人困惑,请在评论中写信给我,我将立即对其进行编辑.

I hope the question is not confusing. If question is confusing, please write me in comments and I will edit it immediately.

谢谢:)

推荐答案

因此,当您转到index页面时,您将获得将放置在主布局中的html片段,并且该html外观像这样:

So, when you go to the index page you will get the piece of html that will be placed in the main layout, and this piece of html look like this:

<div class="container">
    <%= render 'admins/menu' %>
    <%= yield :admin %>
</div>

此代码将正确地yield :admin.

当您进入test页面时,不再有此html代码(因为它仅属于index方法).因此,您放置在content_for(:admin)块中的任何内容都将被忽略,因为没有人正在打印它.

When you go to the test page you do not have this html code anymore (since it only belongs to the index method). So, anything you put in the content_for(:admin) block will be ignored since no-one is printing it.

您可能想要做的是为所有管理页面创建共享布局.遵循本指南,您将找到解决方法.

What you probably want to do is creating a shared layout for all your admin pages. Follow this guide and you'll have your solution.

使用以下方法编辑application.html.erb布局:

Edit the application.html.erb layout using this:

<%= content_for?(:content) ? yield(:content) : yield %>

代替

<%= yield %>

然后在layouts文件夹中创建一个admins.html.erb文件,以处理您的管理页面的布局.像这样:

Then create an admins.html.erb file inside the layouts folder to handle your admin pages' layout. Something like this:

<% content_for :content do %>
  <div class="container">
    <%= render 'admins/menu' %>
    <%= yield %>
  </div>
<% end %>
<%= render template: "layouts/application" %>

会很好的.然后在index.html.erbtest.html.erb中仅放置常规HTML内容,而不使用content_for(:admin)块.一切正常,您将拥有自定义管理模板,外观与常规页面略有不同.

Will do fine. Then in the index.html.erb and test.html.erb just place regular HTML content, without using the content_for(:admin) block. Everything should work fine and you'll have your custom admin template, with a slightly different look from regular pages.

这篇关于使用多个产量插入内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆