ruby - 产量 :content doesn't show content_for :content

标签 ruby ruby-on-rails-4 yield content-for

我想我也有同样的问题。 Using multiple yields to insert content

我尝试了解决方案。我试图拥有 <%= yield :content %>在我的 application.html.erb 中并且有 content_for :contentyield在我看来,里面。但它不适用于我的应用程序。您能否详细解释一下我的问题或提供有关我的问题的示例场景?

内部链接不得重新加载,因此我不会在将显示的每个模板上使用渲染部分。

我最近才开始学习 Rails,所以这对我来说有点困惑。谢谢。

enter image description here

我尝试过这个;这只是一个例子,稍后我会修复侧边栏的连接。

在我的 application.html.erb 中

<!DOCTYPE html>
<html>
<head>
  <title>Clinks</title>
  <%= stylesheet_link_tag    "application", media: "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>

<body>
    <section id="container" >
        <%= render 'layouts/header' %>
        <%= render 'layouts/sidebar' %>
        <section id="main-content">
            <section class="wrapper">
                <div class="row">
                    <!-- %= yield % -->
                    <%=yield(:content) %>
                </div><!--/row -->
            </section><!--/wrapper -->
        </section><!--/main-content -->
    </section><!--/container -->
</body>
</html>

在我的menu_tables.html.erb里面layouts文件夹。

<% content_for(:content) do %>
    <%= render 'menu_tables/sidemenu' %>
    <%= yield %>
<% end %>
<%= render template: "layouts/application" %>

在我的routes.rb中

Rails.application.routes.draw do
  root :to => 'pages#home'
  resources :menu_tables, except: [:show]
end

然后在menu_tables Controller 下的_sidemenu.html.erb中是链接的代码,因此侧边栏的每个链接都有不同的侧菜单。

最佳答案

可能您需要告诉 Controller 使用布局“菜单”。在链接中,您提供了匹配的 Controller 名称和布局,因此它“神奇地”工作了。 请参阅guides :

2.2.14 Finding Layouts

To find the current layout, Rails first looks for a file in app/views/layouts with the same base name as the controller. For example, rendering actions from the PhotosController class will use app/views/layouts/photos.html.erb (or app/views/layouts/photos.builder). If there is no such controller-specific layout, Rails will use app/views/layouts/application.html.erb or app/views/layouts/application.builder. If there is no .erb layout, Rails will use a .builder layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.

(注意:您可以检查日志中使用的布局,查找类似于 Rendered menu_items/index.html.erb inside layouts/application 的行)

关于ruby - 产量 :content doesn't show content_for :content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32001284/

相关文章:

ruby-on-rails - 每次请求都会重新加载模块,因此初始化数据会丢失

ruby - 命令行获取gem路径

Ruby:如何自动添加 "# encoding: UTF-8"?

ruby-on-rails - 查找包含在数字范围内的记录(最小值 - 最大值)

python - 生成器、yield、发送 python

SQL 数据库设计 - 需要不同子表的类型表?

ruby-on-rails - Rails在保存之前验证 child

mysql - 为什么某些 Rails 项目使用多个具有不同扩展名的 database.yml 文件(mysql、postgresql 等)?

ruby - 在 Ruby 中将多个代码块作为参数传递

c# - params 是否可用于通过使用 yield 的函数通过 ref 传递变量