how to conditionally add a div in slim - ruby-on-rails

I have a small html fragment and want to write a conditional such that the first two loading results are wrapped in a div called "loading-top-two" but not sure how to do it. Was thinking something like this but (obviously) doesn't work. How would I do this? On RoR, if there's any magic to be had there.
.loader-results
- (0..2).each do |i|
- if i==0
.loading-top-two
= render "lawyers/search/loading_result", idx: i
- else
= render "lawyers/search/loading_result", idx: i

If I understand correctly you can just do this, first render the first two and then the rest of the result. [2...] means everything from the item with index = 2 until the end (infinity)
.loading-top-two
- #result[0..2].each_with_index do |result, index|
= render "lawyers/search/loading_result", idx: index
- #result[2...].each_with_index do |result, index|
= render "lawyers/search/loading_result", idx: index

Related

Display the number of item

I want to count the number of comment on my page called view
here is my controler code
def view
#gallery = Gallery.find_by!(id: params[:id]).decorate
#comments = Comment.select(:user_id, :description).includes(:user).where(gallery_id: #gallery.id)
if user_signed_in?
#like = current_user.likes.where(gallery_id: #gallery.id).first
end
end
this is view page
.text-container
p: strong Komentar
- #comments.each do |comment|
.media.testimoni-box
.col-md-12.jaminan
.media-heading.strong = comment.user_personal_name
= comment.description
- # want to show the number of comment here
Please help me. and thanks for advance
you can loop the #comments collection with_index and use that as counter
.text-container
p: strong Komentar
- #comments.each.with_index(1) do |comment, count|
.media.testimoni-box
.col-md-12.jaminan
.media-heading.strong = comment.user_personal_name
= comment.description
= count

How to iterate through a collection, and place each item in its own Prawn grid square?

I'm using Prawn in a Rails app to produce a PDF of tiled objects - something like a page of mail merged labels.
I've set up a grid in the pdf document, but am having trouble working out how to iterate through this grid with my collection.
My pdf class looks something like this.
class MyPdf < Prawn::Document
def initialize(vessels, view)
super(page_layout: :landscape)
#objects = objects
define_grid columns: 2, rows: 3, gutter: 10
build_page
end
def build_page
#objects.each do |object|
[0,1].each do |col|
[0,1,2].each do |row|
grid(row,col).bounding_box do
text object.name
end
end
end
end
end
end
This code is obviously not working, and not placing each object in a new grid square and creating new pages as required.
How do I iterate through my objects, and place each one in its own grid square?
(NB. I'm aware of the prawn-labels gem, but it has some limitations so not suitable for my needs. )
The problem is you're writing to each grid location for each item.
Try this instead, this should only write once per object, and create a new page + grid when it needs to.
def build_page
define_grid columns: 2, rows: 3, gutter: 10
col = 0
row = 0
#objects.each do |object|
grid(row,col).bounding_box do
text object.name
if row == 2 && col == 1
start_new_page
define_grid columns: 2, rows: 3, gutter: 10
col = 0
row = 0
else
if col == 1
row += 1
col = 0
else
col += 1
end
end
end
end
end

Render a template inside wysiwyg text

I have a #page.content that is stored in database as a text column. Is there an easy way to embed a render tag inside that html content?
<div>Lorem ipsum</div>
<%= render 'image_slider' %>
<div>Lorem ipsum</div>
I choose the nokogiri way and finished with two urly helpers
def print_content_start( page, shift=4 )
result = ''
doc = Nokogiri::HTML( page.content )
doc.css('div,p').each_with_index do |node, i|
break if i == shift
result += node.to_s
end
result
end
def print_content_end( page, shift=4 )
result = ''
doc = Nokogiri::HTML( page.content )
doc.css('div,p').drop( shift ).each do |node|
result += node.to_s
end
result
end
If anyone knows a better way, please let me know!

Write simple rails code better

I'm newbie on rails.
In my form I get string like "123, xxx_new item, 132, xxx_test "
if the item start with "xxx_" than its mean that i should add the item to the db otherwise enter the value
this is my code and i sure that there is a better way to write this code
tags = params[:station][:tag_ids].split(",")
params[:station][:tag_ids] = []
tags.each do |tag|
if tag[0,4] =="xxx_"
params[:station][:tag_ids] << Tag.create(:name => tag.gsub('xxx_', '')).id
else
params[:station][:tag_ids]<< tag
end
end
I'm looking for how to improve my code syntax
What about:
tags = params[:station][:tag_ids].split(',')
params[:station][:tag_ids] = tags.each_with_object([]) do |tag, array|
array << tag.start_with?('xxx_') ? Tag.create(name: tag[4..-1]).id : tag
end

Rails how to beautify this block

kinda new to Rails.
How would you simplify, beautify this method:
def find_index
index = 0
#ipn.each {|p|
index = 4 if p = "new" #this is just a dummy line
}
index
end
As you can see, it's ugly. How can I remove the index definition at the top and the index at the bottom to return - The Ruby way?
I think you want this method:
http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-find_index
def find_index
#ipn.find_index{|p| p == "new" }
end
I also change the variable name '#ipn' to '#ipns', since it seems to be an enumerable.
And to avoid confusion, I might rename the method to 'find_ipn_index', so it looks like this:
def find_ipn_index
#ipns.find_index{|p| p == "new" }
end
Or, if self.ipns is available, don't define a new method at all for this, just call:
self.ipns.find_index{|p| p == "new" }

Resources