Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4ruby-on-rails-3.1

split a string into two lines in a file .yml


I need split a string into two or three lines in a file .yml. The problem is that if I try to split the last field description in two lines a error is triggered, therefore I leave it a one line

one:
  id: 1
  bus_company_id: 2
  seat_type_id:   1
  description:    Butacas 120º de inclinación,
                  calefacción y aire acondicionado, música funcional y video.

two:
  id: 2
  bus_company_id: 2
  seat_type_id:   2
  description:    Butacas de cuero y paño 120º de reclinación,  bandeja de apoyo
   para pies, calefacción y aire acondicionado, música funcional y video.

three:
  id: 3  
  bus_company_id: 2
  seat_type_id:   4
  description:    Butacas extra ancho de cuero y paño 150º de reclinación, bandeja de apoyo para pies,
   desayuno, almohada y frazada, sistema de DVD y MP3.

four:
  id: 4 
  bus_company_id: 2
  seat_type_id:   5
  description:    Butacas de cuero 180º reclinación, bandeja de apoyo para pies de 180º de reclinación, cortina divisoria, almohada y frazadas sonido sourround, sistema de DVD y MP3, aire y calefacción, menú a elección de carne, pollo o verdura  para su cena.

This error is triggered.

fernando@fernando:~/ProyectoTicketMaster/TicketMaster$ rake db:seed
rake aborted!
ActiveRecord::Fixture::FormatError: a YAML error occurred parsing /home/fernando/ProyectoTicketMaster/TicketMaster/test/fixtures/bus_seat_types.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html
The exact error was:
  Psych::SyntaxError: (<unknown>): could not find expected ':' while scanning a simple key at line 27 column 3
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures/file.rb:43:in `rows'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures/file.rb:29:in `each'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:670:in `block (2 levels) in read_fixture_files'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures/file.rb:20:in `open'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:669:in `block in read_fixture_files'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:668:in `each'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:668:in `read_fixture_files'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:548:in `initialize'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:482:in `new'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:482:in `block (2 levels) in create_fixtures'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:479:in `map'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:479:in `block in create_fixtures'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:232:in `disable_referential_integrity'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/fixtures.rb:476:in `create_fixtures'
/home/fernando/ProyectoTicketMaster/TicketMaster/db/seeds.rb:15:in `<top (required)>'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:245:in `load'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:245:in `block in load'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:245:in `load'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/railties-3.2.19/lib/rails/engine.rb:525:in `load_seed'
/home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/activerecord-3.2.19/lib/active_record/railties/databases.rake:347:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
fernando@fernando:~/ProyectoTicketMaster/TicketMaster$ 

Solution

  • I think you want to use line break formatting described here. It looks like you have two options, here is a quick example that I put together using the ">" notation.

    parse.rb file here:

    require 'yaml'
    foo = YAML.load_file('foo.yaml')
    puts foo
    

    Here is the content of the YAML file:

    one_line: one line of text
    two_lines:
    >
    this is a couple lines of text
    here we go...
    final_line: final line of content
    

    When you run this via command line with ruby parse.rb, you will get the following output:

    {"one_line"=>"one line of text", "two_lines"=>"this is a couple lines of text here we go...\n", "final_line"=>"final line of content"}
    

    Hopefully that makes sense. I think you just need to add the ">" and move the text to the next line.