How to display date in a middleman article page - middleman

I'm developing a blog with middle man, I write the articles in markdown. Below is a sample article 2016-01-27-small-and-large-balcony-ideas.html.md
---
title: Small and large balcony ideas
category: "balcony ideas"
---
# Small and large balcony ideas
It uses the following layout layouts/blog.erb
<body class="<%= page_classes %>">
<%= partial "blog_header" %>
<div class="container">
<div class="row">
<div class="column column-75">
<%= yield %>
</div>
<div class="column column-25">
<%= partial "blog_sidebar" %>
</div>
</div>
</div>
<%= partial "blog_footer" %>
</body>
Now when I go to the article.url the page displays heading and the content straightaway. I want to display the article date above or below the main heading. How can I do that.

I add:
<p><%= current_article.date.strftime('%d %B %Y') %></p>
into my layout just above
<%= yield %>
Is that what you are looking for?

Related

Adding menus in Rails 3

A bit of issue here. Im trying to add a menu to all pages. Reason for this is the ease of editing a single file which updates all web pages.
In my layouts/application.html.erb I have this, between body tags:
<% content_for :menu do %>
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<% end %>
<%= yield %>
And in my welcome/index I have:
<div id="menu">
<%= yield :menu%>
</div>
<h1>Welcome to my index page!</h1>
Not sure if all that is needed so when I go to my root, I only see what is in the welcome/index file and not the links. Am I missing something?
Its should be other way around, that means you could call the :yield in application layout and have the content_for in your index file.
Actually the idea of the content_for tags are to allow slightly different variations for different pages but, still calling the same name from layout. read more about content_for from here
And I think , in your case what you need is a partial in the layout, or even you can have your menu in the layout itself. since the layout is visible for every page your menu will be available for each page, and if you need modifications in later, still you have to change only one page
1 - as a partial
#app/views/layouts/_menu.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
#app/views/layouts/application.html.erb
<%= render partial: 'menu' %>
<%= yield %>
2 - having all in the same file
#app/views/layouts/application.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<%= yield %>
you should have the <%= yield :menu%> in your application.html file.
Also you can make a folder (lets name it "shared") where you can have your menus, messages etc.
Then you can try something like this
#welcome/index.html.erb
<div class="content">
Your Index Content here.
</div>
#layouts/application.html.erb
<%= yield :menu %>
<%= yield %>
#shared/menu.html.erb
<% content_for :menu do %>
<div id="menu">
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
</div>
<% end %>

Pagination inside of tabs

I have 2 tabs with bootstrap for user messages - inbox and outbox, and using kaminari
<div id="allmessages" >
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active">Inbox</li>
<li>outbox</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="inbox">
<%= render #incoming_msgs %>
<%= paginate #incoming_msgs %>
</div>
<div class="tab-pane" id="outbox">
<%= render #outgoing_msgs%>
<%= paginate #outgoing_msgs%>
</div>
</div>
</div>
</div>
Problem is, that when going on page 2 in the inbox and then clicking on the outbox tab I get to page 2 of the outbox..
http://localhost:3000/users/messages?page=2#outbox
is it possible to reverse the order or have a pagination per tab?
Passing the param_name option to the paginate method will allow you to page through each object array independently. Right now they are both using the default param name 'page'.
<div class="tab-pane active" id="inbox">
<%= render #incoming_msgs %>
<%= paginate #incoming_msgs, param_name: :incoming_msgs_page %>
</div>
<div class="tab-pane" id="outbox">
<%= render #outgoing_msgs%>
<%= paginate #outgoing_msgs, param_name: :outgoing_msgs_page %>
</div>
Make sure to reference the new param names in the controller
#incoming_msgs = mailbox.inbox.page(params[:incoming_msgs_page]).per(25)
#outgoing_msgs = mailbox.outbox.page(params[:outgoing_msgs_page]).per(25)
short answer - use ajax pagination
http://railscasts.com/episodes/174-pagination-with-ajax

Articles appear too many times

I started using Ruby on Rails a few days ago, and I have a problem with the each do loops.
Here's my AccueilController's code :
def index
#postsTest = Post.last(1)
#articles = Article.last(1)
#articlesList = Article.last(2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
Here's my application.html.erb code :
<div id="container">
<div id="col1">
<%= render :partial => 'shared/listArticles', :collection => #articlesList %>
<div class="clear"></div>
</div>
<div id="col2">
<%= yield %>
</div>
</div>
</div>
And now my shared/listArticles's code :
<% #articlesList.each do |article| %>
<div id="blocArticle">
<div id="pic">
<%= image_tag (article.photo.url(:thumb)) %>
</div>
<div id="contentArticle">
<div id="titleArticle">
<%= link_to article.title, article %>
<div class="clear"></div>
</div>
<div id="contentArticleDesc">
<%= link_to article.desc, article %>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<% end %>
And my problem is that if I write #articlesList = Article.last(2) then the last two articles will appear two times; if I write #articlesList = Article.last(3) then the last three articles will appear three times etc...
Of course, I would like that each of them appear just one time.
Does someone have any ideas about the source of the problem ?
You're rendering a partial with a collection, therefore Rails calls the partial for each item in the collection. Remove your loop from the partial view or remove the collection param, don't do both!

Rails - Yield a Partial in a Layout

I think this is a very simple problem and I could be just going about it incorrectly because I am new to rails, but here it goes......
What I want to do is render the results of my partial view in my application layout using a yield. I have a search bar at the top of my page that I want the results to display in the yield in the layout. Again, I'm not sure if this is the proper way to do this, so if it isn't, just let me know the best way.
Right now, I get a "Missing template events/search" error when I try and use the search on my page. If I add the following code to controller it gets all of the information properly, but doesn't render in the application layout, it just displays the partial:
render :partial => "events/search_results"
Here is what the relevant section controller looks like:
layout "application"
def search
#events = Event.search params[:search]
end
Here is the application layout (Please excuse the formatting, I tried to make it easily readable):
<!DOCTYPE html>
<html lang="en">
<head>
<title>XXXXXXX</title>
<!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"type="text/javascript"></script> <![endif]-->
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="navbar navbar-top">
<div class="navbar-inner">
<div class="container">
<%= render :partial => "events/search" %>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1">
</br>
<div class="well well-small">
<center><h3>Filters</h3></center> </br>
<%= render :partial => "events/left_severity_filter" %>
<%= render :partial => "events/left_date_filter" %>
</div>
</div>
<div class="span8">
<%= yield %>
</div>
<div class="span3">
<%= render :partial => "events/right_filter" %
</div>
</div>
</div>
Any help would be much appreciated. Thanks.
You do not have the events/search.html.erb template in the views. Create this view template and it will be displayed where the yield is.

Weird rendering of .html.erb file

So I have this file:
<h1>Calendar view</h1>
<div class="events">
<% #events.each do |e| %>
<%= raw(e.content)%>
<% end %>
</div>
<br />
<div class="messages">
<% #messages.each do |m| %>
<%= raw(m.content)%>
<% end %>
</div>
With #events and #messages being valid instance variables in the controller...but when I go to the page the html looks like this:
<h1>Calendar view</h1>
<div class="events">
<br>
<div class="messages">
This is another message test
</div
Event Content
</div>
I'm confused. Maybe I'm missing something obvious?
The problem is that raw() will output raw HTML content. The Rails template engine will try to merge that with the .erb template you supplied.
Therefore, if either m.content or e.content are malformed, you will most likely get unexpected output.
The best way would be to look for syntax errors, especially missing closing elements.