HAML creates unnecessary spaces while using loops - ruby-on-rails

In Rails with HAML, I want to create a simple line of spaces: ====== on the page, with variable length. However, the following code:
- 10.times do
\=
renders the following:
= = = = = = = = = =
I want to get rid of the spaces in the middle of these equals signs, but I don't really have any idea how. I know with ERB you can use
<%= 10.times do -%>=<% end %>
but I'm having trouble figuring out how to do it here.

Another approach, tested, should work:
= "=" * 10

This should work
- 10.times do
&= "="

Related

Array of hashes is overriding data directly to array

I want to make an array of hashes. But the problem is after first iteration when code goes to next line then it directly replaces the content of array.
#item_name =[]
item = {}
#invoiceinfo.each do |invoice|
item[:name] = Invoiceinfo.find(#invoiceinfo.id).item.name
item[:desc] = Invoiceinfo.find(#invoiceinfo.id).desc
item[:unit_price] = Invoiceinfo.find(#invoiceinfo.id).unit_price
byebug
#item_name.push (item)
end
This is what i am getting
after first iteration suppose i have this data
#item_name = [{:name=>"usman", :desc=>"sample ", :unit_price=>100}]
As soon as next line is executed it directly changes #item_name(name variable)
After executing item[:name] = Invoiceinfo.find(#invoiceinfo.id).item.name
the content of the #item_name is changed
#item_name = [{:name=>"next_name", :desc=>"sample ", :unit_price=>100}]
Any help would be appreciated.
Thannks
Try something like this
#item_name = []
#invoiceinfo.each do |invoice|
invoice_info = Invoiceinfo.find(#invoiceinfo.id)
item = {}
item[:name] = invoice_info.item.name
item[:desc] = invoice_info.desc
item[:unit_price] = invoice_info.unit_price
#item_name.push(item)
end
If you consider using ruby paradigms and best practices in ruby code, this mistake won’t happen in the future.
#item_name = #invoiceinfo.each_with_object([]) do |invoice, acc|
invoice_info = Invoiceinfo.find(#invoiceinfo.id)
acc.push(
name: invoice_info.item.name,
desc: invoice_info.desc
unit_price: invoice_info.unit_price
)
end

Removing nested [quote] tags in ruby

I'm writing a forum application in Rails and I'm stuck on limiting nested quotes.
I'm try to use regex and recursion, going down to each matching tag, counting the levels and if the current level is > max, deleting everything inside of it. Problem is that my regex is only matching the first [ quote ] with the first seen [ /quote ], and not the last as intended.
The regex is just a slight tweak of what was given in the docs of the custom bbcode library I'm using (I know very little about regex, I've tried to learn as much as I can in the past couple days but I'm still stuck). I changed it so it'd include [quote], [quote=name] and [quote=name;222] . Could someone examine my code and let me know what the problem could be? I'd appreciate it lots.
def remove_nested_quotes(post_string, max_quotes, count)
result = post_string.match(/\[quote(:.*)?(?:)?(.*?)(?:)?\](.*?)\[\/quote\1?\]/mi)
if result.nil?
return false
elsif (count = count+1) > max_quotes
full_str = result[0]
offset_beg = result.begin(3)
offset_end = result.end(3)
excess_quotes = full_str[offset_beg ..offset_end ]
new_string = full_str.slice(excess_quotes )
return new_string
else
offset_beg = result.begin(3)
offset_end = result.end(3)
full_str = result[0]
inner_string = full_str[offset_beg..offset_end]
return remove_nested_quotes(inner_string , max, count)
end
end
I mean something like
counter = 0
max = 5
loop do
matched = false
string.match /endquote|quote/ do |match|
matched = true
if endquote matched
counter -= 1
else # quote matched
counter += 1
end
if counter > max
# Do something, break or return
else
string = match.post_match
end
end
break unless matched
end

save information from another table

Hi everyone at this time i have two tables:
clientesultimasgestiones
clientesgestiones
And I want to put the whole information from clientesgestiones to clientesultimasgestiones but I want to save it field by field, at this momento I have this
cnx = ActiveRecord::Base.connection
cnx.execute("truncate table clientesultimasgestiones")
#informacion = Clientesgestion.all
#informacion.each do |f|
#clientesultimasgestion = Clientesultimasgestion.new
#clientesultimasgestion.save(f)
Here will be the code to save field by field from clientesgestiones table to the another one
end
Thanks for your help
EDIT: Finally i did it this way:
cnx.execute("truncate table clientesultimasgestiones")
#informacion = Clientesgestion.all
#informacion.each do |f|
l = Clientesultimasgestion.new
l.persona_id = f.persona_id
l.fecha_gestion = f.fecha_gestion
l.clientestipologia_id = f.clientestipologia_id
l.observacion = f.observacion
l.user_id = f.user_id
l.fecha_acuerdo = f.fecha_acuerdo
l.valor_apagar = f.valor_apagar
l.clientestipologiaanterior_id = f.clientestipologiaanterior_id
l.clientesobligacion_id = f.clientesobligacion_id
l.save
end
Thanks a lot :)
I would replace:
#clientesultimasgestion.save(f)
with:
#clientesultimasgestion.update_attibutes(f.attributes)
Also, seems what you want is to copy a table, see https://stackoverflow.com/a/13237661/1197775.
I think this question will help you to get lists of attributes and values.
After this, you need to set dynamically fields, for this purpose you can use method send. Something like this:
#clientesultimasgestion.send("#{field_name}=",field_value)

ruby array serialization not working

I have the following code in my model:
def getFormattedAverages
averages = Array.new();
self.items.each do |i|
x = self.responses.average(:x,:conditions=>['item_id = ?',i.id])
if x.nil?
x = 2000
else
x = x.to_i
end
y = self.responses.average(:y,:conditions=>['item_id = ?',i.id]).to_i
if y.nil?
y = "*"
end
averages.push([[x,y]])
end
return averages
end
In the view I have:
var dataseries = <%=#question.getFormattedAverages%>;
On my development machine, I get the data in exactly the form I need to pass into my graphing function. It looks like this when I "view source" on the rendered page:
var dataseries = [[[31, 34]], [[45, 33]], [[34, 23]], [[10, 27]], [[21, 37]]];
But when I run it on my production server, it looks like this-
var dataseries = -6745-798571322000010791-2270-18;
Note that the x and y data on my development and production servers is different. The point is that all of the brackets and commas are being stripped out. Any help you can provide would be much appreciated - this one really has me stumped!
I found this answer.
Changing the code in my view to read
var dataseries = <%=raw #question.getFormattedAverages.to_json%>;
seems to work!

Ruby loop problems

I am working on a script that is supposed to be writing a list of items to a hash, but for some reason its only placing the last item in the loop in the hash... I have been working on this script all day, so I am pretty sure its something I am just missing.
Here is the script
#mr = MediaRating.where("user_id = ?", session['user_credentials_id'])
#mr.each do |rating|
#m = Media.where("id = ?", rating.media_id)
#m.each do |m|
s = Profile.find_by_subscriber_id(m.subscriber_id)
#h_lang = Language.find_by_code(s.language)
#history = {m.title => #h_lang.english}
end
end
There are multiple records in the MediaRating table so I know it has to do something with how my loop is. Thanks in advance for the help!
Working code:
#mr = MediaRating.where("user_id = ?", session['user_credentials_id'])
#mr.each do |rating|
#m = Media.find(rating.media_id)
s = Profile.find_by_subscriber_id(#m.subscriber_id)
#h_lang = Language.find_by_code(s.language)
#history[#m.title] = #h_lang.english
end
In the last line, you are over-writing the entire #history hash instead of adding a new key/value pair to it. I'm guessing that isn't what you intended. Change this line:
#history = {m.title => #h_lang.english}
to this:
#history[m.title] = #h_lang.english

Resources