Scanning text to find domain types
I'm trying to piece together a part of a create action in a controller that scans the the text entered and intelligently understands what type of domain name it is.
I have a text box called "domain_names". A user puts domains into the box separated by commas, e.g. "yahoo.com, google.com"
In the controller it hits it like this:
@extracted_domains = (params[:domain_names]).split(",") @extracted_domains.each do |domain| domain.strip domain_scan = domain.scan(/(\w+)[.]/).flatten com_scan = domain.scan(/[.](\w+)/).flatten new_domain_type = DomainType.find_or_create_by_domain_type(:domain_type => com_scan) new_domain = Domain.create(:domain => domain_scan, :domain_type_id => new_domain_type.id) end
In the console it works great. But when I put it into practise I get really odd things stored in the database. For example if :domain was meant to have the value "google", it will instead have the value "---\n- google\n" , when its stored in the database.
No idea why
Thanks in advance.
Problem: It was putting an array into a string.
Solution: Make it a string.
domain_scan = domain.scan(/(\w+)[.]/).flatten.first com_scan = domain.scan(/[.](\w+)/).flatten.first
As for your issue, can we see the exact params that are sent?
I would take a look at https://github.com/pauldix/domainatrix for domain extraction.
It appears to be fed YAML input. Three dashes at the beginning of the string followed by a newline are a strong indicator of YAML: http://en.wikipedia.org/wiki/YAML#Sample_document