rails: specify list of items in yml

13,000

Solution 1

not sure if this helps, but when i try to load the first example, it works for me. Maybe the indentation is not correct?

anyway, this works here:

require "YAML"

something = YAML.load_file("admin.yaml")

oh yes, let me add the admin.yaml that works for me:

admin:
  - action: create
    resource: employee
  - action: show
    resource: company

Solution 2

If you're having trouble generating YAML, I would try building an object in the console, then converting it to YAML to see what it looks like. For example:

test = { :admin => [
           {:action => "create", :resource => "employee"},
           {:action => "show", :resource => "company"}
          ] }

test.to_yaml
 => "--- \n:admin: \n- :action: create\n  :resource: employee\n- :action: show\n :resource: company\n" 

You can even output it to a file if it makes your life easier:

File.open('test.yaml', 'w') do |out|
  out.write(test.to_yaml)
end

Which yields:

--- 
:admin: 
- :action: create
  :resource: employee
- :action: show
  :resource: company

I haven't quite matched what you have above, since I used symbols for keys, but this should help you out I hope.

Share:
13,000
Maddy.Shik
Author by

Maddy.Shik

Updated on June 09, 2022

Comments

  • Maddy.Shik
    Maddy.Shik about 2 years

    I want to specify resource authorisation info in yml file. admin can create an employee and can only view company.

    I used YAML::load method to load this file.

    If i use - symbol for multiple permission (action, resource pair) it gives parsing error. If i remove - symbol then it only picks first action resource pair. I think load method expect 1 space indentation while parsing and if i specify - then one space indentation condition is violated that is reason for error. What is possible solution for this.

    if i use - symbol for listing

    admin:
     - action: create
       resource: employee
     - action: show
       resource: company
    

    if i do not use - symbol for listing

    admin:
     action: create
     resource: employee
     action: show
     resource: company