Rails each loop insert tag every 6 items?
You can use Enumerable#each_slice
in conjunction with #each
to avoid inline calculations. each_slice
breaks the array into chunks of n, in this case 6.
<% @images.each_slice(6) do |slice| -%>
<div class="gallery">
<% slice.each do |image| -%>
<%= image_tag(image.url, :alt => image.alt) %>
<% end -%>
</div>
<% end -%>
Ruhoh - Insert Tag every x items
I'm fairly new to ruby myself, however I think this solution meets your needs!
Create a new file in the plugin
directory called pages_collection_view_addons.rb
(if it doesn't already exist).
Add this to that file:
module PagesCollectionViewAddons
def chunks(n = 3)
# Get all the pages
pages = all
chunks = []
# Split the 'pages' array into chunks of size n
pages.each_slice(n) { |slice|
chunks.push({pieces: slice})
}
chunks
end
end
# Inform Ruhoh of this new addon
Ruhoh::Resources::Pages::CollectionView.send(:include, PagesCollectionViewAddons)
In your template add something such as:
{{# posts.chunks}}
<div class="row">
{{# pieces }}
<h1>{{ title }}</h1>
{{/ pieces }}
</div>
{{/ posts.chunks }}
This will iterate over each of the chunks where each chunk looks like:
{pieces: [post1, post2, post3]}
Hope this helps.
Rails - How to put some data inside looping rails
Rails automatically provides a counter for your collection partial that is named name_of_model_counter (post_counter
, in your case). So, you could use that to insert data only when the counter is a certain number. i.e add this to the bottom of your partial...
...
<% if post_counter % 3 == 0 %>
<div class="col-lg-12">
# insert data here
</div>
<% end %>
you can use a case statement (or similar) inside the div to determine the exact value of post_counter
to render different data for each of them.
The important part is that post_counter
exists and can be used to check which iteration you are on.
Edit for comment question:
Assuming you will know how much data there will be ahead of time, you can add another condition like this...
# replace x with the iteration number you want to stop at
<% if (post_counter <= x) && (post_counter % 3 == 0) %>
Insert items into an array while iterating
Actually, your code works, you just need to replace print d
with print a[i]
since what you're printing is the variable d
not the actual array element at index i
Rather than deleting and inserting, why not change the element on that index?
a = "1234567890".split("")
a.each_with_index{|d, i|
if d.eql?('5')
a[i] = ['A','B','C']
end
print a[i] #a[i] rather than d, since d contains the old value
}
or
...
if d.eql?('5')
a[i] = ['A','B','C']
d = a[i]
end
print d
Deleting/Inserting on an Array during iterations is discourage since it may cause headaches haha... Resort to other methods if possible :)
Note:
I've just used the current logic in your code based on my understanding, and the given desired output
the array will become [1,2,3,4,['A','B','C'],6,7,8,9,0] and not [1,2,3,4,'A','B','C',6,7,8,9,0]. If you want the other, just leave a comment :)
If what you want is just to change a value in the string, you can just use .tr
or .gsub
to do the job
Render n objects at a time, inserting code then repeating for the next n objects - Rails
<% @gifts.each_slice(3) do |slice| %>
<div class="row-fluid>
# slice now is a 3 element array, iterate over it and render as you see fit
</div>
<% end %>
I'm more of a HAML guy, but this should work.
Loop between a div and a div title= Page in rails
Try
<%= raw cycle('<div title="Pages">', '<div>') %>
or
<%= cycle('<div title="Pages">', '<div>').html_safe %>
How do I loop through a model and save that to an array?
User.where(:state => 'Arizona').each do |u|
emails = u.email
end
In the above logic, in every iteration, the emails will be overwritten with the iteration email.
You have to insert the emails in an array to make the logic as expected.
emails = []
User.where(state: 'Arizona').each do |u|
emails << u.email # To insert the user email into the array
end
You can also try to debug the values in the array whenever you face issues in logic.
User.where(:state => 'Arizona').each do |u|
emails = u.email
p emails, "emails" # To check the value stored in the emails.
end
Related Topics
What Is the Use of Gemfile in Rails
How to Clear the Terminal in Ruby
Iterate Through Every File in One Directory
Ruby/Rails - Change the Timezone of a Time, Without Changing the Value
Undefined Method Attr_Accessible
Rails Installation Failed on Ubuntu With "Cannot Load Such File - Mkmf"
Rails Assets:Precompile Yarn Executable Was Not Detected in the System
How to Parse a CSV File, Update a Field, Then Save
Change the Name of the :Id Parameter in Routing Resources for Rails
How to Search a Folder and All of Its Subfolders For Files of a Certain Type
Rails Browser Detection Methods
Read Binary File as String in Ruby
Why Not Use Shared Activerecord Connections For Rspec + Selenium
Ruby Local Variable Is Undefined
How to Format a Date in Ruby to Include "Rd" as in "3Rd"
Google Plus API Shutdown Today, Which Alternative Can Be Used to Authentication