Pagination inside of tabs - ruby-on-rails-3

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

Related

How to display date in a middleman article page

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?

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

SQL Query to Rails 3 for average user ratings

I have a rails app that rates restaurants on specific dimensions using the letsrate gem. I'd like to calculate the average of all ratings for each restaurant and display it to the user in an array on the index page.
My SQL query would look like this -
select avg(stars) from RESTAURANTS r, RATES rs
where rs.rateable_id = r.id
group by r.name;
The array in my index looks like -
<% #restaurants.each do |restaurant| %>
<li>
<a href="<%=restaurant_path(restaurant) %>" >
<div class="left">
<h2 class="name"><%= restaurant.name %></h2>
<h3 class="location"><%= restaurant.location %></h3>
</div>
<div class="right">
<h4 class="rate">AVERAGE RATING</h4>
</div>
<div class="clear"></div>
</a>
</li>
<% end %>
Wondering how I would translate the sql query into rails to display the averages in the array.
If you setup your relations correctly, this should work. If not I'll help you fix them.
Edit
In your Restaurant Controller:
class RestaurantController > ApplicationController
def index
#restaurants = Rate.joins(:restaurant).select("avg(rates.stars) as res_avg, restaurants.name, restaurant.location").group("restaurants.name")
end
end
In your Restaurant index.html.erb:
<% #restaurants.each do |restaurant| %>
<li>
<a href="<%=restaurant_path(restaurant.id) %>" >
<div class="left">
<h2 class="name"><%= restaurant.name %></h2>
<h3 class="location"><%= restaurant.location %></h3>
</div>
<div class="right">
<h4 class="rate"><%= restaurant.res_avg %></h4>
</div>
<div class="clear"></div>
</a>
</li>
<% end %>
Edit2
If you want to re-use this query declare it in a scope on the model.
class Restaurant < ActiveRecord::Base
#all your model code
scope :avg_restaurant_rates, joins(:rate).select("avg(rates.stars) as res_avg, restaurants.name, restaurants.location").group("restaurants.name")
end

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!

Render partial with content input in rails 3

I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>