How to wrap every N elements in parent div in ERB (Rails)? - html

My members index page is simply a list of members, but I'd like every 3 members to be wrapped in a containing div (that will act like a row). So rather than:
<div class="member"></div>
<div class="member"></div>
<div class="member"></div>
<div class="member"></div>
<div class="member"></div>
I need the markup to be:
<div class="row">
<div class="member"></div>
<div class="member"></div>
<div class="member"></div>
</div>
<div class="row">
<div class="member"></div>
<div class="member"></div>
</div>
I do have a solution, but I'm not happy with it. I have actually seen a better way to do it in ERB before, but can't find it again.
My current code:
<div class="row">
<% #members.each do |member| %>
<div class="member"><%=member.name%></div>
<%= cycle("", "", "</div><div class=\"row\">".html_safe) %>
<% end %>
</div>

How about this:
<% #members.each_slice(3) do |slice| %>
<div class="row">
<% slice.each do |member| %>
<div class="member">
...your markup here
</div>
<% end %>
</div>
<% end %>

I found the method I was looking for. It's basically identical to each_slice() posted by #HargrimmTheBleak, but has a more friendly name:
in_groups_of()

Sounds like a great chance to use a modulus
<div class="row">
<% for(i=1, i<=#members.size, i++ %>
<% if i%4 == 0 %>
</div>
<div class="row">
<% end %>
<div class="member"><%=#members[i-1]%></div>
<% end %>
</div>

Related

Rails 7 url_for(article.header_image) background-image not appearing

I have the following code for showing the header image in my index.html.erb for my articles:
<% #articles.each do |article| %>
<% if article.public? %>
<%= link_to article_path(article), class: 'text-decoration-none' do %>
<div class="card mt-3 post-card">
<div class="card-body">
<div class="row">
<div class="col-md-2">
<% if article.header_image.present? %>
<div class="header-img"
style="background-image: url(<%= url_for(article.header_image) %>);">
</div>
<% end %>
</div>
<div class="col-md-10">
<h5 class='card-title mb-1 text-dark'><%= article.title %></h5>
<p class='text-secondary mb-0'><%= article.body %> </p>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
<% end %>
I am following the tutorial by Techmaker Studio, and the desired output is at this timestamp: https://youtu.be/MCEzxY9BbiU?t=5059
The code is correct because I inspected and see the example image (the ruby icon) shows up in the inspector but not in the background image.
inspect screenshot proves url is working
The one difference between my project and the tutorial I am following is that I am using Rails 7 and importmaps. Does anyone have an idea why it isn't showing up? when I use image_tag, it works but not with the background-image style I desire.

Move pagination to footer

I trying to move pagination to footer on my website.
I don't want to ready solution, I would like to make this alone, but I do not know where to start, how can a real programmer handle this?
*I finished this site lately from book, and now i trying to do something alone.
code:
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="box">
<div class="panel panel-default">
<%= link_to image_tag(pin.image.url(:medium)), pin %><br/>
<div class="panel-body">
<%= pin.description %><br/>
<br/>
<strong><%= pin.user.name if pin.user %></strong><br/>
</div>
<% if pin.user == current_user %>
<div class="panel-footer">
<%= link_to 'Edit', edit_pin_path(pin) %><br/>
<%= link_to 'Delete', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
<br/>
<div class="center">
<%= will_paginate #posts, renderer: BootstrapPagination::Rails %>
</div>
screen:
You havent showed your containers. If you pass all content in order and into containers:
<div class="container">
<div class="row">
<div class="col-md-12">
Stff..
</end>
</end>
</end>
All should be fine. As a fast fix try to:
<div class="center col-md-12">
<%= will_paginate #posts, renderer: BootstrapPagination::Rails %>
</div>
But this just a fast fix, not solution...
And your code is a bit messy, and you have one closing div, but havent showed where it was opened

Formatting with overlapping divs and bootstrap to form 2 columns

I'm trying to make 2 nice columns of "Food Categories" populated with foods.
This is what I have so far. I cant figure out how to give the divs to go next to each other. I'm making my first rails app with bootstrap.
JSFiddle
Here is the template code I currently have for the page:
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em;">
0%
</div>
</div>
<h3>List of all Categories</h3><br>
<% #categories.each_with_index do |category, i| %>
<div style=""<% if i % 2 == 0 %> class="col-md-offset-1" <% end %> <% if i % 2 == 1 %> class="col-md-offset-7" <% end %>>
<h4><%= category.name %></h4>
</div>
<% category.foods.each do |food| %>
<div style=""<% if i % 2 == 0 %> class="col-md-offset-2" <% end %> <% if i % 2 == 1 %> class="col-md-offset-8" <% end %>>
<%= food.name %>
</div>
<% end %>
<% end %>
<hr>
<%= button_to "Start Survey", survey_category_selection_path, class: "pull-right", method: :get %>
You have to wrap each category in a div. Then, you put .col-md-6 class. So each block will take 50% of the width.
You will have something like this
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em;">
0%
</div>
</div>
<h3>List of all Categories</h3><br>
<% #categories.each_with_index do |category, i| %>
<div class="col-md-6">
<h4><%= category.name %></h4>
<% category.foods.each do |food| %>
<div class="col-md-offset-1">
<%= food.name %>
</div>
<% end %>
</div>
<% end %>
<hr>
<%= button_to "Start Survey", survey_category_selection_path, class: "pull-right", method: :get %>
JSFiddle
Ok, what you are asking is to align the div tags so that they appear side by side:
div {
width: 48%;
display: inline-block;
}
You may instead want to select .inline-block instead of div.
Please note that you need to keep the width less than 50% because your indentation will make the div tags not appear line.
Also, make a media query so that when the page gets smaller the div tags will appear as block, not inline block.
hope this helps.

Bootstrap columns within columns for iteration over an object

I want to create a pair of bootstrap columns inside another bootstrap column.
My output is not as desired for some reason.They look completely goofy for some reason. Did I make a rookie mistake in my formatting?
I am iterating over a #user object and having the profile picture be on the left and the profile info be on the right. There are many users so it should ideally style for all the iterations of the users in the database.
I feel this code should work but it doesn't, any idea as to what happened:
<div class="row">
<div class="other-box col-sm-6">
<p>Random text!!!</p>
</div>
<div class="index-user-container col-sm-6">
<div class="row">
<% #users.each do |user| %>
<div class="index-picture col-sm-4">
<%= image_tag user.image.thumb('150x185#').url if user.image_stored? %>
</div>
<div class="index-info-box col-sm-8">
<%= user.first_name %>
<%= user.last_name %>
<%= user.email %>
<%= link_to 'Go to profile', user_path(#user) %>
</div>
<% end %>
</div>
</div>
</div>
I am using twitter bootstrap. Here is a screen shot of the goofyness:
If you want a new row for each user in the list, then put the iterator above the row div, not inside it - otherwise, you might get some funky results.
<div class="index-user-container col-sm-6">
<% #users.each do |user| %>
<div class="row">
<div class="index-picture col-sm-4">
<%= image_tag user.image.thumb('150x185#').url if user.image_stored? %>
</div>
<div class="index-info-box col-sm-8">
<%= user.first_name %>
<%= user.last_name %>
<%= user.email %>
<%= link_to 'Go to profile', user_path(#user) %>
</div>
</div>
<% end %>
</div>

div inside form rails

I have these markup on my rails application these form is not working when i tr
<div class="span5 mg myform">
<div class="span5 mg">
<ul class="clearfixremo formmenu">
<li class="lefty blocky boldy rightbrd"><i class="icon-pencil"></i> Post</li>
<li class="lefty blocky boldy myphotoupload "> <i class="icon-camera"></i> Photo</li>
</ul>
</div>
<div class="span5 mg">
<div class="row">
<div class="span4">
<%= simple_form_for(current_user.posts.new, :remote => true ,:html => { :multipart => true } ,:class=>"form-horizontal" ) do |f| %>
<div class="field">
<%= f.text_area :body ,:rows=>1%>
<%= f.select :privacy,["public","friends","only me"] %>
</div>
</div>
</div>
</div>
<div class="span5 mg">
<div id="pactions" class="actions">
<%= f.submit "#{t 'share'}",:id=>"share"%>
</div>
<% end %>
</div>
</div>
it shows to me on html markup that the form is closed before the select menu and when I move the submit button before the select menu and inside the field div its working
Fix the formatting and indentation of your ERB and the problem should become obvious: opening tags (including <%= ... do %>) must match their closing tags (including <% end %>). You can't:
<div>
<form>
</div>
</form>
you need to:
<div>
<form>
</form>
</div>
Your <form> opens one <div>
<%= simple_form_for(current_user.posts.new, :remote => true ,:html => { :multipart => true } ,:class=>"form-horizontal" ) do |f| %>
<div class="field">
and then tries to close four of them:
</div>
</div>
</div>
</div>
When the browser sees that invalid HTML, it will attempt to fix it by closing the <form> behind your back and it will pretend that you said:
</div>
</form>
</div>
</div>
Once that happens, your form is broken and nothing works the way you're expecting it to.
Fix your tag nesting so that everything is closed in the opposite order that the tags are opened. And start formatting your code properly so that the structure is obvious, the computer won't care but you will.