Adding menus in Rails 3 - ruby-on-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 %>

Related

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

Reaching into an associated collection

I have a user model, and event model, and an asset model.
A user can have multiple events.
An event can have multiple assets (images)
Now what I am trying to do is display one (any) event image on a page. Currently I have the following.
Controller
#user = current_user
#events = #user.events
View
<% #events.each do |e| %>
<li>
<% if e.assets.nil? %>
<%= image_tag("img36.jpg" , :size => "280x230") %>
<% else %>
<%=image_tag e.assets.first.path.url %>
<% end %>
<div class="bar">
<strong class="heading"><%= e.name %></strong>
<ul class="menu">
<li><a class="time" title="Time" href="#"><%= e.date %></a></li>
<li><a class="comments" title="Comments" href="#">53</a></li>
<li><a class="favourites" title="Favourites" href="#">87</a></li>
<li><a class="view" title="Views" href="#">242</a></li>
</ul>
</div>
<p><%= e.description %>more ยป</p>
</li>
<% end %>
"path"is the string field in the assets table that contains the image path. I'm getting the following error right now.
undefined method `path' for nil:NilClass
Any ideas? Thanks!
The problem is when there are no assets associated with an event, the value of e.assets will be an empty array, not nil. So what is happening is that e.assets passes the nil? conditional, then you take e.assets.first which is nil (because the array is empty), and then you try calling path on that which obviously doesn't work.
To fix the problem just change:
<% if e.assets.nil? %>
to:
<% if e.assets.empty? %>

Is it possible to pass html elements into a partial?

I have a partial that contains a header, a subheader and potentially one or more buttons. It is used on many different pages. Often the pages don't need buttons, so I just pass in the header and an optional subheader to the partial. However sometimes the pages need one or more buttons and the only way I've managed to allow for an arbitrary number of buttons to be passed in is using content_for. My partial looks like this:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= content_for :page_header_buttons %>
</ul>
</header>
<% end %>
This use of content_for nasty. Is there any way I can pass the list items / buttons into this partial? How else could I deal with this situation?
You could transform this partial into a layout:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= yield %>
</ul>
</header>
<% end %>
And you would render it like that:
<%= render layout: "your_partial_above", locals: { page_title: "page title } do %>
<%= render "partial_with_your_buttons" %>
<% end %>

GET request and Fancybox working with rails

I'm pretty sure theoretically what I am trying to do should work and I believe my syntax is correct. But it would be good to get some fresh eyes on it.
I want to pull in articles from a site (GET request) and display those articles in a fancybox, so my users wouldn't need to leave the site. What am I missing here?
Thanks for your help on this.
<ul class="box_qa">
<% #hero_news.each do |item| %>
<li>
<%= item["webTitle"] %> <%= link_to "Read more!", item["webUrl"], :class=>"fancybox" %>
<span class="timestamp">
Posted <%= time_ago_in_words(item["webPublicationDate"])%> ago.
</span>
</li>
<% end %>
</ul>
<script>
$(document).ready(function(){
$("a.fancybox").fancybox({'type': 'image'});
});
</script>
Switched "image" to iframe. Stil having sizing issues however.

Hide Page Elements From View

I have a blog set up with the usual articles and tags on the index page. The Tags section also shows up on the sign-in and sign-up pages which doesn't look great.
The tags are set up in their own column under _side.html.erb - is there a way to hide them from all other pages apart from the index page?
<h3>Blog Tags</h3>
<div id= "tags" >
<% cache('all_tags') do %>
<% for tag in Tag.find(:all, :order => 'name') %>
<ul style="list-style-type: none">
<li>
<%= link_to "#{tag.name}", tag_path(tag) %>
</li>
</ul>
<% end %>
</div>
If you
render :partial => 'side'
from within your application.html.erb, then you could use another layout for the index page which then renders the partial while the standard layout would not.