Iterate 4 Arrays with Rails - ruby-on-rails

I'm new to Rails and I'm trying to build a nice application and I'm struggling with arrays, I have 4 arrays that I want to iterate and they are not the same size
I want to generate sections in HTML using the first array what I did it
#sections = ['Section One','Section Two','Section Three','Section Four']
#itemsOne = ['item 1','item 2','item 3','item 4','item 5','item 6']
#itemsTwo = ['item 1','item 2','item 3','item 4','item 5','item 6']
I was using
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t1, t2, t3| %>
<%= t1 %>
<table>
<tbody>
<tr>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
</tr>
</tbody>
</table>
<% end %>
I have a table that have a Section Title and cells that have two values
but what I get is the value of |t2| in each cell of |t1| section
using #Phil answer down below, but he deleted it.
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t| %>
<%= t[0] %>
<table>
<tbody>
<tr>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
</tr>
</tbody>
</table>
<% end %>
p.s. the itemsOne and itemsTwo arrays have more than 20 values.

What I created is separated my big arrays into smaller ones and then Iterated thru each this way without a Table because table was making design issues so i went into div's using bootstrap 3 column, there might be a better way but this is what i got as a beginner.
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[0] %></h4>
<!-- This will Display Section 0 in the Array -->
</div>
<div class="row">
<div class="col-md-12">
<% #count = 0 %>
<!-- Counter is Zero -->
<% #ItemsOne.collect do |t1| %>
<!-- This will loop array to increment the #count and repeat the HTML -->
<% #count += 1 %>
<!-- With each loop increment by 1-->
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #ItemsOne[#count - 1] %>
<!-- Counter should start from 0 adding -1 will make it start
from 0 instead of 1 and then will print the value of the Index Number -->
</label>
</div>
</div>
<% end %>
</div>
</div>
</div>

Here is another way to do this
<div class="row">
<% #Sections.each_with_index do |x1, n| %>
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[n] %></h4>
</div>
<div class="row">
<div class="col-md-12">
<% if n == 0 %>
<% #itemsOne.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsOne[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 1 %>
<% #itemsTwo.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsTwo[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 2 %>
<% #itemsThree.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsThree[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 3 %>
<% #itemsFour.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsFour[n] %>
</label>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>

Related

Rails 5 yield gives duplicate contents

I am figuring out why it showing duplicate data on the front page:
All I did is to use Rails collection rendering.
Here's my application.html.erb file:
<body>
<%= render 'layouts/navbar' %>
<!-- content -->
<main class="pt-5">
<div class="container">
<div class="row">
<%= render 'layouts/sidebar' %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</main>
</body>
</html>
Here's my index.html.erb file:
<div class="col-md-9">
<% if #contacts.blank? %>
<div class="alert alert-warning">
<strong>Record is empty!</strong>
</div>
<% else %>
<div class="card">
<div class="card-header"><strong>All Contacts</strong></div>
<table class="table">
<%= render #contacts %>
</table>
</div>
</div>
<% end %>
and here's my _contact.html.erb file:
<% #contacts.each do |contact| %>
<tr>
<td class="middle">
<div class="media">
<div class="media-left">
<a href="#">
<%= image_tag contact.gravatar, class: "media-object" %>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><%= contact.name %></h4>
<address>
<strong><%= contact.address %>, <%= contact.city %>, <%= contact.state %>, <%= contact.zip %></strong><br>
<%= contact.email %> | <%= contact.phone %> | <%= contact.mobile %>
</address>
</div>
</div>
</td>
<td width="100" class="middle">
<div>
<a href="#" class="btn btn-outline-secondary btn-circle btn-xs" title="Edit">
<i class="fa fa-edit"></i>
</a>
<a href="#" class="btn btn-outline-danger btn-circle btn-xs" title="Delete">
<i class="fa fa-times"></i>
</a>
</div>
</td>
</tr>
<% end %>
BTW, I am using Kaminari Gem for pagination. It works however the data shows twice.
Any idea?
UPDATES:
Here's my contacts controller:
class ContactsController < ApplicationController
def index
#contacts = Contact.page params[:page]
end
end
Here's the kaminari config file:
# frozen_string_literal: true
Kaminari.configure do |config|
config.default_per_page = 2
# config.max_per_page = 4
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
# config.params_on_first_page = false
end
When you do <%= render #contacts %>, Rails will loop results from #contacts and render each one using _contact.html.erb, while embedding local variable contact in each one. You see contacts twice, because you loop twice: remove <% #contacts.each do |contact| %> from _contact.html.erb and it should work.

Update or Remove Item from Cart?

I manage the orders made by the customers. My Order table is connected to the model Cart. The model Cart is connected to the Product model Many to Many which gave the association table line_items which includes id_cart, id_product, and quantity. But for the Cart, I want to add the option to update and delete an element of the Cart. I met a blocking - more precisely in the view Order where at the end I show the list of the elements in the Cart as follows
<h3> Order Details </ h3>
<table class = "table-bordered table">
<td> Designation </ td> <td> Quantity </ td> <td> Price </ td> </ tr>
<% order.cart.line_items.each do | item | %>
<tr align = "center">
<td> <% = f.text_field: product_id,: value => item.product.title,: readonly => true, class: 'form-control'%> </ td>
<td> <% = f.text_field: qte,: value => item.qte, class: 'form-control'%> </ td>
<td> <% = number_to_currency (product.inc.unit, unit: "F", separator: ",", format: "% n% u")%> </ td>
</ Tr>
<% end%>
</ Table>
There I will have the fields textfield with default value what the customer had ordered allowing the modification. But the problem is that the attibutes product_id and qte do not belong to the model order but earlier to the model line_items, so I will not have permission to insert:
unpermitted parameters: :product_id, :qte
I even try the method accepts_nested_attributes_for and I will have permission. But the transaction will not commit but rollback.
And thank you for helping me for this bug that prevents me from advancing for days
the submitted params look like this
<%= form_with(model: order,:remote => true) do |f| %>
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="header">
<h4 class="title"> Edite Order</h4>
</div>
<div class="content">
<div class="row">
<!-- Order Reference-->
<div class="col-md-5">
<div class="form-group">
<label><%= f.label :ref,"Reference Order "%></label>
<%= f.text_field :ref, :value => "ABC-123-NY#{#order.id}",:readonly => true,class: ' form-control' %>
</div>
</div>
<!-- Supplier List -->
<div class="col-md-5">
<div class="form-group">
<label><%= f.label :supplier_id,"Fournisseur"%></label>
<%=f.collection_select :supplier_id,Fournisseur.all,
:id,:namefrs,{required: true},id:"listfrs",class: 'form-control'%>
</div>
</div>
<!-- boutton update -->
<div class="col-md-2">
<div class="form-group">
<%= f.submit 'Modifier',:name => "valid",:class => 'btn btn-info btn-fill pull right'%>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="content">
<div class="row">
<!-- Date Order -->
<div class="col-md-5">
<div class="form-group">
<label><%= f.label :date_order,"Date"%></label>
<%= f.date_select :date_order, label: false, placeholder: "SĂ©lectionnez la date et l'heure",class: 'form-control'%>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col" id="cart">
<h3>Order</h3>
<table class="table table-bordered">
<tr align="center" class="h4">
<td>Designation</td>
<td>Quantity</td>
<td>Price</td>
</tr>
<% order.cart.line_items.each do |item| %>
<tr align="center">
<td><%= f.text_field :product_id,:value => item.product.title,:readonly => true,class: 'form-control'%></td>
<td><%= f.text_field :qty, :value=>item.qty ,class: 'form-control' %></td>
<td><%= number_to_currency(item.product.price, unit: "F",separator:",",format:"%n %u")%></td>
</tr>
<% end %>
</table>
</div>
<% end %>
i have this in my controller Order
def update
respond_to do |format|
#order.cart.line_items.each do |item|
item.qty = params[:qty]
end
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
end
end

how make a dynamic bootstrap carousel in rails?

I need show a list of products in a carousel using bootstrap grouped by 4 elements in each 'item' class.
here is my develop
<div class="carousel-inner" role="listbox">
<% products.each_with_index do |product, n| %>
<% if n % 4 == 0 %>
<div class="item <%= 'active' if n == 0 %>">
<% end %>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
...
</div>
<% if n % 4 != 0 %>
</div>
<% end %>
<%end%>
</div>
the problem is when i try close 'item' class. I can't catch the opposite of multiples of 4.
<div class="carousel-inner" role="listbox">
<% products.each_slice(4).with_index do |slice, index| %>
<div class="item <%= 'active' if index == 0 %>">
<% slice.each do |product| %>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
...
</div>
<% end %>
</div>
<% end %>
</div>

Show maximum symbols in ruby on rails?

How to show maximin 250 symbols? For post.description
<% #microposts.each do |post| %>
<% a = post.had %>
<% b = post.needs.to_f %>
<% count = (a/b)*100 %>
<div class="col-md-12 post card-1 card-3">
<h3 class="pad-h3"><%= post.title %> <text class="needs"> Needs <%= post.needs %>$</text></h3>
<div class="row pad">
<div class="col-md-6"><a href="/help/id<%= post.id %>"><img class="img2" src="<%= post.image1 %>">
<h3 class="fun"><%= count %>% Funded</h3>
</a></div>
<div class="col-md-6"><p><%= post.description %></p></div>
</div>
<div class="row">Show more information</div>
</div>
<% end %>
Sometext SometextSometextSometextSometext Sometextv Sometextv vvvvvvSometextSometextSometextSometextSometextSometextSometextSometextSometextSometextSometextSome textSometextS dometext SometextSometextSometextSomete xtSometextSometextSometextSometextSometextSometext
Rails has truncate text helper:
<%= truncate post.description, length: 250 %>

Ability to only see the content of the profile that has been filled out

so I've created a web app that has user profiles, where users can search for fellow users based on interests etc.. However sometimes the users don't fill out certain areas of their profile, i.e. about me may be left blank.. So how can I add the ability for users to only see the content of the profile that they filled out. In other words they can’t see the parts of other user profiles for which they have not completed.
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>
The conditionals that you have can be expanded to cover the whole div containing the info block.
For example:
<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>
Could be
<div class="addy">
<% if current_user.address.present? %>
<div class="span2">
<%# User address %>
<i class="fa fa-home"></i>:
<%= current_user.address %>
</div>
<% else %>
<%= link_to('Add Address', edit_account_path) %>
<% end %>
</div>
Then it would only show the link if the address is not present.
Seems like you're on the right track. Basically, for every user attribute on the User model you want to show/hide, you'll want to add a conditional that evaluates whether or not the attribute on current_user is present?:
<span class="name1">
<% if current_user.first_name.blank? %>
<%= link_to('Finish your profile', edit_account_path) %>
<% else %>
<%= current_user.first_name %> <%= current_user.last_name %>
<% end %>
</span>
Alternatively, you can continue using ternary operators for single line evaluations, as you are already doing in some instances:
<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %>

Resources