Create toggle-able Bootstrap tabs from Ruby hash - ruby-on-rails

So I'm trying to create toggle-able tabs from a Ruby hash. For example, let's say that I have the following hash:
tabs = {
:Friends =>
[
[name: 'john', age: 20, sex: 'M'],
[name: 'elma', age: 21, sex: 'F']
],
:Family =>
[
[name: 'father', age: 50, sex: 'M'],
[name: 'mother', age: 48, sex: 'F'],
[name: 'sister', age: 17, sex: 'F']
]
}
This would result in the two tabs 'Friends' and 'Family' respectively which would list either friends or family members as an unordered list.
I would like to make this tabs toggle-able without Ajax if possible, so basically if I click the Friends tab it would access the appropriate value from the tabs[:Friends] hash.
Thanks!

Try this if it help you
Link to ur partial
<div id="toggle_div" style="display:none">
<%= render partial => "ur_partial" %>
</div>
By using JQuery
<script type="text/javascript">
$(function(){
$('.toggleLink').click(function(){
$('#toggle_div').toggle();
});
});
</script>
add different IDs to both tabs and toggle them...

Found the solution yesterday:
<div class="tabbable tabs-left">
<ul class="nav nav-tabs">
<li>
All
</li>
<% tabs.each do |k,v| %>
<li>
<a href="#<%= k.to_s %>" data-toggle="tab">
<%= k.to_s %>
</a>
</li>
<% end %>
</ul>
<div class="tab-content">
<div class = "tab-pane active" id = "all_tabs">
<ul>
<% tabs.each do |k,v| %>
<li>
<%=k%>
</li>
<% end %>
</ul>
</div>
<% tabs.each do |k,v| %>
<div class = "tab-pane" id = "<%= k.to_s %>">
<strong><%= k.to_s %></strong>
<ul>
<% v.each do |key,val| %>
<li>
<%=val.name%>
</li>
<% end %>
</ul>
</div>
<% end %>
</div>
</div>
Here is the generated html output:
http://www.bootply.com/0sWdURMEPY

Related

Smart_listing Gem sorting

Im integrating Smart_listing gem onto my Rails App. I have a Beer table in my database and a beer_type column. Right now im trying to set up a sorting function where Users can filter different types of beer. Similar to this functionality example.
Right now it will filter each one type of beer perfectly. However whenever there is more than 1 beer type checked, it would not display the beer types together.
product.rb
scope :with_beer_type_ales, -> (product) { where('beer_type ILIKE?', "%ales%") }
scope :with_beer_type_cream, -> (product) { where('beer_type ILIKE?', "%cream%") }
scope :like, ->(args) { where("name ILIKE? OR beer_type ILIKE? OR description ILIKE?", "%#{args}%", "%#{args}%", "%#{args}%")}
products_controller.rb
def index
products_scope = Product.all
# Make users initally sorted by name ascending
products_scope = products_scope.like(params[:filter]) if params[:filter]
products_scope=products_scope.with_beer_type_ales(params[:with_beer_type_ales]) if params[:with_beer_type_ales] == "1"
products_scope = products_scope.with_beer_type_cream(params[:with_beer_type_cream]) if params[:with_beer_type_cream] == "1"
#products = smart_listing_create :products,
products_scope,
partial: "products/list",
page_sizes: [10, 20, 30],
default_sort: {name: "asc"}
end
index.html.erb
<%= smart_listing_controls_for(:products, {class: "form-inline text-left"}) do %>
<div class="form-group filter input-append" id="search">
<%= text_field_tag :filter, '', class: "search form-control",
placeholder: "Search...", autocomplete: :off %>
<button class="btn btn-primary disabled" type="<%= :submit %>">
<span class="glyphicon glyphicon-search"></span>
</button>
</div>
<h4>Sort By Type:</h4>
<ul>
<li class="checkbox">
<label class="checkbox inline">
<%= hidden_field_tag :with_beer_type_ales, "0" %>
<%= check_box_tag :with_beer_type_ales %> Ales
</label>
</li>
<li>
<label class="checkbox inline">
<%= hidden_field_tag :with_beer_type_cream, "0" %>
<%= check_box_tag :with_beer_type_cream %> Cream
</label>
</li>
</ul>
</div>
<% end %>
You want OR not AND. When you specify multiple where clauses in a chain, rails defaults to AND.
q = Beer.where(true)
q = q.or.where('beer_type ILIKE?', "%ales%") if params[:with_beer_type_ales] == "1"
q = q.or.where('beer_type ILIKE?', "%cream%") if params[:with_beer_type_cream] == "1"

Add as favorite with no full page refresh

I am working on a Hiragana flashcards app.
I spend nights and days to understand how don't refresh full page when I add a flashcard (hiragana) as a favorite.
Here is the favorite controller
class FavsController < ApplicationController
def index
#favs = Fav.where(user: current_user)
end
def create
#hiragana = Hiragana.find(params[:hiragana_id])
#fav = current_user.favs.new(hiragana: #hiragana)
if not #hiragana.favs.where(user: current_user).take
#fav.save
end
render json: #fav
end
def destroy
#fav = Fav.find(params[:id])
#fav.destroy
redirect_to :back
end
end
I render json in the create method and when I click on view I add only an hash
render view favorite
<% if current_user %>
<div class="hiragana-fav">
<% if hiragana.is_faved_by(current_user) %>
<%= link_to fav_path(hiragana.is_faved_by(current_user)), method: :delete do %>
<i class="fa fa-heart faved faved-on"></i>
<% end %>
<% else %>
<%= link_to hiragana_favs_path(hiragana), method: :post do %>
<i class="fa fa-heart faved faved-off"></i>
<% end %>
<% end %>
</div>
<% end %>
and it is located in hiragana render
<div class="row">
<ul class="list-inline text-center card-frame">
<li>
<div class="card">
<div class="front">
<% if current_user.try(:admin?) %>
<%= link_to hiragana_path(hiragana), class:'trash-hiragana', data: { confirm: 'Are you sure?' }, method: :delete do %>
<%= image_tag("delete-btn.png") %>
<% end %>
<% end %>
<span class="card-question img-popover" data-content="<h4 class='text-center letter-uppercase'><%= hiragana.bigletter.upcase %></h4><p class='text-center'><b><%= hiragana.midletter %></b> comme dans <b><%= hiragana.transcription %></b></p>">
<i class="fa fa-eye fa-lg"></i>
</span>
<div class="card-hiragana hiragana-<%=hiragana.bigletter.downcase.last%>">
<h1><b><%= hiragana.ideo1 %></b></h1>
</div>
<div class="card-katakana">
<p><%= hiragana.ideo2 %></p>
</div>
<%= render 'favs/favorites', hiragana: hiragana %>
</div>
<div class="back">
<div class="col-sm-3 col-xs-4 col-md-3 containerbackcards-<%=hiragana.bigletter.downcase.last%>">
<div class="backcard-hiragana">
<h1><b><%= hiragana.ideo1 %></b></h1>
</div>
<div class="card-bigletter">
<h4><%= hiragana.bigletter.upcase %></h4>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
When I add a card as favorite it gives me a hash like this :
{
id: 64,
user_id: 1,
hiragana_id: 4,
created_at: "2016-02-10T16:37:26.270Z",
updated_at: "2016-02-10T16:37:26.270Z"
}
I just want to have the heart grey to red as favorite, saved and not refresh the entire page. Your explainations are appreciated thank you.
In order to send the request to the controller without the page refreshing you need to use a combination of Ajax and JavaScript.
You use JavaScript to add a click listener to the .faved-on button and to trigger the Ajax request. You will also use JavaScript to prevent the default action occurring when you click the link ie. The Page Refresh
You then use Ajax to send the request to the controller and handle the response.
Your initial JavaScript code looks pretty correct, except you are missing the bit to stop the page from reloading.
See the e.preventDefault(); line below
$(document).ready(function() {
$('.faved-on').click(function(e) { //make sure to pass in the e (the event paramter)
e.preventDefault(); //this is the line you are missing
var fav = $('.faved-off')
//let your ajax handle the rest then
$.ajax({
type: "POST", url: "/hiraganas", dataType: "json",
success: function(data) {
console.log(data);
//change the color of your heart to red here
},
error: function(jqXHR) {
console.error(jqXHR.responseText);
}
});
})
})
I haven't tested your JavaScript but it looks pretty close to correct, I believe its the e.preventDefault(); bit you were missing

Rails will_paginate and tabs

I have a problem with working my tab along with will_pagination. When i press Tab 2, it will show the correct content, but when i press on the pagination link, example page 2. It will bring me back to my Tab 1 view. After this, when i press back Tab 2, it will appear page 2 content of Tab 2. The problem here is why does it bring my back to my Tab 1 view when i press on the pagination link in Tab 2.
My View Code
<div>
<ul class="nav nav-tabs" id="TabLength">
<% #biscuit.each_index do |i| %>
<% if i == 0 %>
<li class="active"><a data-toggle="tab" href="#<%= #biscuit[i] %>"><%= #biscuit[i] %></a></li>
<% else %>
<li><a data-toggle="tab" href="#<%= #biscuit[i] %>"><%= #biscuit[i] %></a></li>
<% end %>
<% end %>
</ul>
<div class="tab-content">
<% #biscuit.each_index do |i| %>
<% if i == 0 %>
<div class="tab-pane fade in active" id="<%= #biscuit[i] %>">
<div class="row" id="PictureSetting">
<% #testpage.each do |i| %>
<div class="col-md-4" id="ProductPic">
<%= image_tag(i.name , class:"img-thumbnail", size:"180x180") %>
</div>
<% end %>
</div>
<%= will_paginate #testpage, :param_name => 'user_page' %>
</div>
<% elsif i == 1 %>
<div class="tab-pane fade" id="<%= #biscuit[i] %>">
<div class="row" id="PictureSetting">
<% #memberpage.each do |i| %>
<div class="col-md-4" id="ProductPic">
<%= image_tag(i.name , class:"img-thumbnail", size:"180x180") %>
</div>
<% end %>
</div>
<%= will_paginate #memberpage, :param_name => 'choco_page' %>
</div>
<% else %>
<div class="tab-pane fade" id="<%= #biscuit[i] %>">
<div class="row" id="PictureSetting">
<h1>hello</h1>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
Thanks
Based on answer from here, you can design the following code for the problem:
<script>
// open tab on click
$('#TabLength a').click(function (e) {
e.preventDefault();
$(this).tab('show');
// getting tab id
var id = $(e.target).attr("href").substr(1);
// change hash value for all pages
$('ul.pagination > li > a').each(function(i, pageAnchor) {
pageAnchor.hash = id;
});
});
// assign tab id to location hash
$("ul.nav-tabs > li > a").on("shown.bs.tab", function (e) {
var id = $(e.target).attr("href").substr(1);
window.location.hash = id;
});
// open initial hash
var hash = window.location.hash;
$('#TabLength a[href="' + hash + '"]').tab('show');
// UPDATE
$('ul.pagination > li > a').each(function(i, pageAnchor) {
pageAnchor.hash = hash;
});
</script>
It saves currently selected tab into location.hash. and selects it when you navigate to a new page.

favouriting users profiles in rails

so I've created a web app that has user profiles, where users can search for fellow users based on interests etc.. How might I add a feature where users can favorite a persons profile? I.e. User A finds User B and likes what they see, and can click a 'Favorite this profile' button and perhaps it's starred, and stored? What would the code look like for this? Just an idea, but I'm open to all ideas.
user_profile.html.erb
<%= render "shared/header" %>
<div id="landing_welcome_page">
<div class="container">
<div class="row">
<%#= Profile image upload %>
<div class="span4">
<%= user_avatar(current_user) %>
<%#= Space w line %>
<div class="name"></div><br>
<%#= Please bare in mind these are strickly temporary placeholders i.e whitespace %>
<%= render 'social' %>
</div>
<div class="span8">
<%# User name %>
<span class="name1">
<% if current_user.first_name.blank? %>
<%= current_user.first_name.present? ? current_user.first_name : link_to('Finish your profile', edit_account_path)%>
<% else %>
<%= current_user.first_name %> <%= current_user.last_name %>
<% end %>
</span>
<span class="side-buttons">
<div class="name"></div>
</span>
</span>
</div>
<div class="row">
<br />
<div class="span6">
<%# User occupation %>
<i class="fa fa-usd"></i>:
<%= best_in_place current_user, :occupation, nil: 'Add occupation' %>
</div>
<div class="addy">
<div class="span2">
<%# User address %>
<i class="fa fa-home"></i>:
<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %>
</div>
</div>
<div class="span6">
<%# User gender %>
<br />
<% if current_user.gender == "M" || "male" %>
<i class="fa fa-male"></i> Male
<% else %>
<i class="fa fa-female"></i> Female
<% end %>
</div>
<div class="span2">
<!-- Code to calculate age by user birthday -->
<br />
Age: <%= user_birthday %>
</div>
<div class="span8"></div>
<div class="span8"><div class="name"></div></div>
<div class="span8">
<div class="tabbable"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li class="active">About me</li>
<li>Photos</li>
<li>Personality</li>
</ul>
<div class="tab-content">
<div class="tab-pane in active" id="tab1">
<% #questions_for_about.each_with_index do |question, index| %>
<div class="question">
<h4 class="user_questions">
<%= index + 1 %>. <%= question.question %>
<%= link_to ("<i class='icon-edit'></i>".html_safe),
edit_user_question_path(current_user, question),
remote: true, class: "edit_link_#{question.id}" %>
</h4>
<div class="answer" id="answer_<%= question.id %>">
<%= answer_for(question) %>
</div>
</div>
<% end %>
</div>
<div class="tab-pane" id="tab2">
<div class="page-header">
<%= form_for Photo.new do |f| %>
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add photos...</span>
<%= f.file_field :file, multiple: true, name: "photo[file]" %>
</span>
<% end %>
<div class="clearfix"></div>
</div>
<div class="photos_cont">
<div class="col-sm-6 col-md-3">
<span class="gallery"><%= render current_user.photos %></span>
</div>
</div>
</div>
<div class="tab-pane" id="tab3">
<% #questions_for_personality.each_with_index do |question, index| %>
<div class="question">
<h4 class="user_questions">
<%= index + 1 %>. <%= question.question %>
<%= link_to ("<i class='icon-edit'></i>".html_safe),
edit_user_question_path(current_user, question),
remote: true, class: "edit_link_#{question.id}" %>
</h4>
<div class="answer" id="answer_<%= question.id %>">
<%= answer_for(question) %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%= render '/shared/footer' %>
</div>
</div>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%;"></div></div>
</div>
</script>
<script>
var fb_param = {};
fb_param.pixel_id = '6009056882201';
fb_param.value = '0.00';
(function(){
var fpw = document.createElement('script');
fpw.async = true;
fpw.src = '//connect.facebook.net/en_US/fp.js';
var ref = document.getElementsByTagName('script')[0];
ref.parentNode.insertBefore(fpw, ref);
})();
</script>
<noscript>
<img height="1" src="https://www.facebook.com/offsite_event.php?id=6009056882201&value=0" style="display:none;" width="1"/>
</noscript>
<script type="text/javascript">
// remove default datepicker event
jQuery(document).off('best_in_place:datepicker');
jQuery(document).on('best_in_place:datepicker', function(event, bip, element) {
// Display the jQuery UI datepicker popup
jQuery(element).find('input')
.datepicker({
format: element.data('date-format')
})
.on('hide', function(){
bip.update();
})
.on('changeDate', function(){
$(this).datepicker('hide');
})
.datepicker('show');
});
</script>
Create a Favorite model with a has_and_belongs_to_many relationship with User
Once you create the Favorite model and set up the relationship, you can do things like:
#user.favorites = [ user1, user2, user3 ]
#user.favorites << user4
to assign favorites to a user, or display them with something like:
<%= #user.favorites.map(&:name).to_sentence %>
You'll find everything you need to know on how to do this here: http://guides.rubyonrails.org/association_basics.html

Rails - How to create alphabetical system?

I am getting data from my mySQL database with Ruby on Rails like that:
def all_specs
Specialization.order("title ASC").all;
end
Now, I would like to sort this data in the view file like that:
<div class="nav-column">
<h3>A</h3>
<ul class="submenu">
<li>All data that has title that starts with A</li>
</ul>
</div>
<div class="nav-column">
<h3>A</h3>
<ul class="submenu">
<li>All data that has title that starts with B</li>
</ul>
</div>
and so on from A-Z
How can I achieve that ?
You could group the data:
specs = Specialization.order("title ASC")
specs_by_first_letter = specs.group_by { |spec| spec.title[0] }
This returns a hash like:
{
"A" => [<Specialization title:"A...">, <Specialization title:"A...">],
"B" => [<Specialization title:"B...">, <Specialization title:"B...">],
...
"Z" => [<Specialization title:"Z...">, <Specialization title:"Z...">]
}
Looping through this hash should be quite easy. Note that some letters could be missing.
Thank you very much for all of your help. Zippie provided amazing solution!
Here is the implementation and iterating over hash for everyone:
<%
institutions_c = all_institution_categories
inst_by_first_letter = institutions_c.group_by { |inst| inst.title[0] }
%>
<% inst_by_first_letter.each do |key, value| %>
<div class="nav-column">
<h3><%= key %></h3>
<ul class="submenu">
<% value.each do |cat| %>
<li><%= link_to cat.title, institutions_path(:categories => cat.id), :title => cat.title %></li>
<% end %>
</ul>
</div>

Resources