2

I think I have the same issue with this. Using multiple yields to insert content

And I tried the solution. I tried to have <%= yield :content %> in my application.html.erb and have content_for :content and the yield inside, in my view. But it is not working on my app. Can you please explain more or give sample scenario on my problem?

The links inside, must not reload, so I will not use render partial on every template I will display.

I just recently started learning Rails so it was all a little bit confusing for me. Thank you.

enter image description here

I tried this; this is just an example I will fix the connection of sidebar later.

in my applicaion.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>

in my menu_tables.html.erb inside layouts folder.

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

in my routes.rb

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

then in _sidemenu.html.erb under menu_tables controller is the code for the links, so each links from sidebar have different sidemenu.

Community
  • 1
  • 1
do_Ob
  • 709
  • 1
  • 6
  • 24

1 Answers1

2

May be you need to tell your controller to use layout 'menu'. In the link you provided names of the controller and layout matched, so it worked 'magically'. See 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.

(Note: you can check what layout is used in logs, look for line similar to Rendered menu_items/index.html.erb within layouts/application)

faron
  • 1,051
  • 7
  • 9
  • Thank you very much for this, it's make sense now. BIG HELP!!! But still not the answer I need. And I am thinking to go in `render partial` on each view. – do_Ob Aug 14 '15 at 08:20
  • 1
    I'm afraid I didn't get what you actually want to make. Can you described it in other words? Is it different partial rendered in white links box for different page from sidebar? – faron Aug 14 '15 at 11:43
  • Yes, you're right ^_^ each link from the sidebar have different links (*menu_links, inside links with the white background*) And if I clicked the link from menu_links, I don't want is to be reloaded. Is that possible? sorry for my english. I'm not good also at explaining ^_^ but the answer you gave is big help also.. thanks for that. – do_Ob Aug 17 '15 at 00:56