yield :content doesn't show content_for :content - ruby

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.
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.

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)

Related

javascript_include_tag not working in rails 2?

The rails tag to include javascript , and for browser caching:
<%= javascript_include_tag '/skin/js/html5.js', :cache => true %>
forget about caching simple javascript_include tag is also not working.
Ex:
<%= javascript_include_tag '/skin/js/html5.js', :cache => true %>
But the script tag below is working find stop working after doing this.
<script src="/skin/js/html5.js" type="text/javascript"></script>
using Rails 2 any reason for such a behavior or this tag did not even work in rails2?
Try
First make sure that you are including the tag inside the head
<% content_for :head do %>
<script src="skin/js/html5.js"></script>
<% end %>

Beego template render partials

Is there a functionality to render partial templates in the Beego? I used this functionality in RoR.
Ruby on Rails partials:
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
Example from RoR:
<!-- html.erb -->
<h1>New zone</h1>
<%= render partial: "form", locals: {zone: #zone} %>
and
<!-- _form.html.erb -->
<%= form_for(zone) do |f| %>
<p>
<b>Zone name</b><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Yes, though they are not explicitly referenced as partials in the documentation, you can compose templates like you described.
From the documentation for views.
{{template "header.html"}}
Logic code
{{template "footer.html"}}
Because Beego is an MVC framework, it will automatically look for your "partials" in the views directory.
If you had another subdirectory called views/base then your partial would look like this:
{{template "base/header.html" .}}
A good reference implementation that will provide more examples in the context of a real world app, check out the wetalk project on github

Rails how can I put <%= link_to ...%> into an instance variable?

I have this (and other) navigation links:
<li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> |
These need to be put into the header part of my templates and they change based on the view template.
So in application.html.erb I have this:
<div class='submenu'>
<%= #app_menu %>
</div> <!-- end submenu -->
And in the view template I have:
#app_menu =
The question is, how do I put the top line (with the link_to, etc) into that instance variable and have Rails parse it properly with the HTML and without errors. Right now if I include <ul><li> it prints them out literally. And, of course, if I include <%= link_to... I get all sorts of errors.
Or is there perhaps a better way?
What I ended up doing was creating partials for each menu, so one partial _actors_menu.html.erb might look like this:
<%= image_tag "submenu-separation.png", :alt => 'separator' %> <li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
<li><%= link_to "Actors with Ampersand (&)", actor_path('search')+'/acute' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
Then I put #app_menu = "actors_menu" on each actor view. And so on for the other pages.
But I'm wondering if there is a smarter way to do all this?
Also, where and how would I set a global variable for the app_menu so as not to repeat it in every template?

Conditional navigation bar

Hello Guys I've a request for you.I've Drinks, Menus and Users routes.
I've this code in views/layout/application.html.erb:
<div>
<%= render "shared/navigation_bar"
</div>
How to make the navigation_bar to render in all the pages except in users pages?
I finally work around many methods, but I finally come up with a solution(not really stunnin', I must confess) , that works for me.
<div>
<% if controller.controller_name == "drinks" %>
<%= render "shared/navigation_bar" %>
<% elsif controller.controller_name == "menus" %>
<%= render "shared/navigation_bar" %>
<% end %>
</div>
The flow control doesn't include Users's controller, so the navigation_bar layout won't appear on User's pages.
That's it.If you guys have a better alternative just let me know.

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>
<%= yield :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.

Resources