sam.roberts55 sam.roberts55 - 6 months ago 12
Ajax Question

rails ignore adding to array

I have this bunch of code here:

doc.xpath("//script[@type='text/javascript']/text()").each do |text|
if text.content =~ /more_options_on_polling/
price1 = text.to_s.scan(/\"(formatted_total_price)\":\"(.+?)\"/).uniq
description = text.to_s.scan(/\"(ticket_desc)\":\"(.+?)\"/).uniq
price = price1 + description
testing = price1.map{|a| { a[0] => a[1] } }
desc = description.map{|a| { a[0] => a[1] } }
respond_to do |format|
format.json { render :json => {:testing => testing,
:desc => desc }}
end
end


Now in the desc, It returns this:

"desc": [
{
"ticket_desc": "Later Owl Ticket"
},
{
"ticket_desc": "Later Owl Ticket+Collector Ticket &#64 extra £4.95 per ticket"
},
{
"ticket_desc": "Later Owl + Chance For VIP Upgrade"
},
{
"ticket_desc": "VIP Ticket"
},
{
"ticket_desc": "VIP Ticket + Collector Ticket &#64 extra £4.95 per ticket"
},
{
"ticket_desc": "Skydeck Package"
},
{
"ticket_desc": "5 Person Skydeck Table"
},
{
"ticket_desc": "7 Person Skydeck Table"
},
{
"ticket_desc": "10 Person Skydeck Table"
}
]


What im wanting is anything with a + symbol inside to be ignored. so ideally the response would be this:

"desc": [
{
"ticket_desc": "Later Owl Ticket"
},
{
"ticket_desc": "VIP Ticket"
},
{
"ticket_desc": "Skydeck Package"
},
{
"ticket_desc": "5 Person Skydeck Table"
},
{
"ticket_desc": "7 Person Skydeck Table"
},
{
"ticket_desc": "10 Person Skydeck Table"
}
]


I'm currently making this with rails, But i'm renderign it into the view via javascript/Ajax, So if you have any ideas what i can do there let me know :)

Heres the ajax:

for (var i = 0; i < json.testing.length; i++) {
var section = json.testing[i].formatted_total_price;
var desc = json.desc[i].ticket_desc;

Answer

Well, one option is to use select on the desc hash. For example, after the line that begins with desc = you could add:

desc.select! { |a| !a['ticket_desc'].include?("+") }

If you want to keep the original desc, use the non-destructive select and put the results in a new variable:

desc_wo_plus = desc.select { |a| !a['ticket_desc'].include?("+") }