【问题标题】:split a string into two lines in a file .yml将字符串拆分为文件 .yml 中的两行
【发布时间】:2015-01-31 11:58:24
【问题描述】:

我需要将一个字符串拆分为文件 .yml 中的两三行。问题是,如果我尝试将最后一个字段描述分成两行,则会触发错误, 所以我把它留了一行

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.

触发此错误。

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$ 

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.1


    【解决方案1】:

    我想你想使用here 描述的换行格式。看起来您有两个选择,这是我使用“>”表示法放在一起的一个简单示例。

    parse.rb 文件在这里:

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

    这是 YAML 文件的内容:

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

    当您通过命令行使用ruby parse.rb 运行此程序时,您将获得以下输出:

    {"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"}
    

    希望这是有道理的。我认为您只需要添加“>”并将文本移动到下一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多