Basic string formatting in Rails view using loops - ruby-on-rails

Hi newb to Rails here.
I want to format a string to fit into an html div. The model I'm after is;
Break the string into an array seperated by whitespace.
If any array item length is greater than 22
Split the characters into lengths of 22 and display.
else separate the words by whitespace and then display.
It's for handling long names on a guestbook type application.
<% $p = 0 %>
<% #m = name.split(" ") %>
<% while $p < #m.size do %>
<% if #m[$p].length > 22 %>
<%= name.slice(0, 21) %><br>
<%= name.slice(21, 43) %><br>
<% else %>
<% $i = 0 %>
<% #x = name.split(" ") %>
<% while $i < #x.size do %>
<%= #x[$i] %><br>
<% $i +=1 %>
<% end %>
<% end %>
<% $p +=1 %>
<% end %>

(1..(name.length / 22)).each { |i| name[22*i] = ' ' } if name.length > 22

Related

Group where no appointment

The code below shows number of appointment each hour and 'No appointment' if no appointment is available.
I want to group consecutive 'No appointment' slot according to time
for e.g., The output of the current code is -
10:00 AM to 11:00 AM -> No Appointment
11:00 AM to 12:00 PM -> No Appointment
12:00 PM to 01:00 PM -> No Appointment
I want the output to be
10:00 AM to 01:00 PM -> No Appointment
The code is give below:-
<% #time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in #time_group
<% #time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% #booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
#if appointment is available show appointment else show no appointment
<% if #booked.size > 0 %>
<% #booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
No Appointment
<% end %>
<% end %>
First of all, one might prepare the array:
#time_group = ['6:00:00','7:00:00', ...]
#stacked = #time_group.each_with_object([]) do |start, memo|
range = Time.parse(start)..Time.parse(start)+3600
booked = Appointment.where(start_time: range)
if memo.empty? || !booked.empty? || !memo.last.last.empty?
memo << [range, booked] # no sequenced empty slots
else
memo << [memo.pop.begin..range.end, booked] # merge ranges
end
end.to_h # { ranges => booked }
And now—let’s output them:
<% #stacked.each do |range, booked| %>
.....
<% end %>
Try like this
<% #time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in #time_group
<% #time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% #booked = Appointment.where(start_time: t1..t2) %>
#It will holds the No Appointment Timings into it
no_appointment_array = []
#if appointment is available show appointment else show no appointment
<% if #booked.size > 0 %>
#Calling the Function
display_no_appointment(no_appointment_array) if not no_appointment_array.empty?
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<% #booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
no_appointment_array << [Time.parse(t).strftime('%I:%M %p'), (Time.parse(t) + 3600).strftime('%I:%M %p')]
<% end %>
<% end %>
def display_no_appointment(no_appointment_array)
puts "#{no_appointment_array.first[0]} to #{no_appointment_array.last[1]} -> No Appointment"
#It Clears all Previous No Appointment
no_appointment_array.clear
end
First of all I suggest you to use if '#booked.present?' instead of 'if #booked.size > 0'
Have you tried something like this?
I have not test this code, it is almost an idea to a possible solution.
Hope that it could be helpful for you.
<% no_appointment_starts_at = nil %>
<% no_appointment_ends_at = nil %>
<% #time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% #booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
#if appointment is available show appointment else show no appointment
<% if #booked.present? %>
<% #booked.each do |app| %>
<% unless no_appointment_ends_at.blank? %>
<%= Time.parse(no_appointment_starts_at).strftime('%I:%M %p') %> to <%= (Time.parse(no_appointment_ends_at) + 3600).strftime('%I:%M %p') %>
<% no_appointment_ends_at = nil%>
<% end %>
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<%#= app.start_time.strftime('%I:%M') %>
<% no_appointment_starts_at = t2 %>
<% end %>
<% else %>
<% last_appointment_ends_at = t2 %>
<% end %>
<% end %>

How can I get the model name of object in rails?

I got four different models.
Here's an example,
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
then how to check which model #production is?
I tried
<% #production.each do |p| %>
<%=p.class.name%>
<% end %>
but it returns "Array"
It seems to be simple, but I can't find out
(I edited question)
The problem is here
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
change these lines with
#single = Single.all.to_a
#coe = Coe.all.to_a
#blend = Blend.all.to_a
#production = #single+#coe+#blend
and then if you will check
#production.first.class.name #Single
#production.last.class.name #Blend
so in your view you can do this
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
if while iterating on #production it returns array so you need to try this.
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
#production is a collections of combinations of single, coe, and blend thats why #production.class.name doesnt work, you need to iterate each object like this:
<% #production.each do |object| %>
<%= object.class.name %>
<% end %>

Custom method in model - undefined local variable

I have a rooms model that looks like follows:
class Room < ActiveRecord::Base
belongs_to :host
has_many :bookings
def space
if self.bookings.empty?
return self.capacity
else
return (self.capacity - total_booked)
end
end
def total_booked
total_booked = 0
room.bookings.each { |booking| total_booked += booking.number_of_guests if
((booking.start_date >= #start_date && booking.start_date <= #end_date) ||
(booking.end_date >= #start_date && booking.end_date <= #end_date)) }
return total_booked
end
end
In my view I'm trying to call on these methods like so:
<% host.rooms.each do |room| %>
<!-- display information on free rooms -->
<% if room.bookings.empty? %>
<p>room #<%= room.id %> is available (0 booked, <%= room.capacity %> free out of <%= room.capacity %>)</p>
<% else %>
<% if !(room.space == 0) %>
<p>room #<%= room.id %> is available (<%= room.total_booked %> booked, <%= room.space %> free out of <%= room.capacity %>)</p>
<% end %>
<% end %>
<% end %>
Unfortunately this gives me the error: undefined local variable or method 'room' for the following line:
<% if !(room.space == 0) %>
I think there might be a syntax error somewhere since I've called on model methods from a view file before but I can't spot it. Any ideas?
Edit:
full error log:
undefined local variable or method `room' for #<Room:0x00000003a714f0>
Extracted source (around line #25):
22 <% if room.bookings.empty? %>
23 <p>room #<%= room.id %> is available (0 booked, <%= room.capacity %> free out of <%= room.capacity %>)</p>
24 <% else %>
25 <% if !(room.space == 0) %>
26 <p>room #<%= room.id %> is available (<%= room.total_booked %> booked, <%= room.space %> free out of <%= room.capacity %>)</p>
27 <% end %>
28 <% end %>
def total_booked
total_booked = 0
room.bookings.each { |booking| total_booked += booking.number_of_guests if
((booking.start_date >= #start_date && booking.start_date <= #end_date) ||
(booking.end_date >= #start_date && booking.end_date <= #end_date)) }
return total_booked
end
room is not defined in this context. You probably meant self.bookings or just bookings.

how to compare two conditions in each loop

In code:
<% #line_item.each do |line_item| %>
<% id=line_item.menu_id%>
<% #menu1=Menu.find(id)%>
<%= #menu_name = #menu1.menu_item_name%>
<% #offers.each do |offer| %>
**<% if offer.menuName_buy1 == #menu_name && offer.menuName_buy2 == #menu_name %>**
I am new in rails.I want to compare menuName_buy1 & menuName_buy2 with #menu_name, but how do i achieve this result.At a time i am getting 1 value for #menu_name.

Timezone and group

My problem is there are 160 entries published at today but i see only 13 entries when i print entries_count. I guess it is about timezone. My timezone is Athens. Any help will be appreciated. I spent my whole day to fix this.
#from_date = Time.zone.now.beginning_of_day
#to_date = Time.zone.now.end_of_day
#entries_by_date = Entry.where(:published_at => #from_date..#to_date).group("date(published_at)").select("date(published_at) as date, count(*) as entries_count")
view
% (#from_date.to_date..#to_date.to_date).each do |day| %>
<% a= #entries_by_date.detect {|entry| entry.date == day} %>
<% if a %>
<%= a.entries_count %>
<% else %>
0
<% end %>
<% end %>
Update:
Time.zone.now can be any date Time.zone.now is for example.
You just want to match on day/month/year. Try this:#entries_by_date = Entry.where(:published_at => #from_date..#to_date).
group("year(published_at), month(published_at), day(published_at)").
select(" year(published_at) as year,
month(published_at) as month,
day(published_at) as day,
count(*) as entries_count")
then the view:
<% (#from_date.to_date..#to_date.to_date).each do |date| %>
<% a= #entries_by_date.detect {|entry| entry.day == date.day &&
entry.month == date.month &&
entry.year == date.year} %>
<%= a.try(:entries_count) || 0 %>
<% end %>

Resources