Rails: specify a list of elements in yml

I want to specify resource authorization information in a yml file. admin can create an employee and can only view the company.

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

If I use - a symbol for multiple resolution (action, a couple of resources), it gives a parsing error. If I delete a character, then it selects the first pair of action resources. I think that the loading method expects 1 indentation in space during parsing, and if I specify, then one condition of the indentation of the space is violated, which is the cause of the error. Why is this possible.

if I use - a character to list

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 
+6
source share
2 answers

Not sure if this helps, but when I try to download the first example, it works for me. Maybe the indentation is wrong?

Anyway, this works here:

YAML required

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

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

  admin:
   - action: create
     resource: employee
   - action: show
     resource: company
+4
source

If you are having trouble creating YAML, I would try to create an object in the console and then convert it to YAML to see what it looks like. For instance:

 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 

What gives:

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

I did not quite understand what you have, because I used the characters for the keys, but this should help you, I hope.

+3
source

Source: https://habr.com/ru/post/888946/


All Articles