开发者

rails: specify list of items in yml

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开发者_Python百科 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜