Rails:指定 yml 中的项目列表

发布于 2024-11-09 15:12:25 字数 472 浏览 5 评论 0原文

我想在 yml 文件中指定资源授权信息。管理员可以创建员工,只能查看公司。

我使用 YAML::load 方法来加载该文件。

如果我使用 - 符号表示多个权限(操作、资源对),则会出现解析错误。如果我删除 - 符号,那么它只选择第一个操作资源对。我认为加载方法在解析时期望 1 个空格缩进,并且如果我指定 - 则违反了 1 个空格缩进条件,这就是错误的原因。有什么可能的解决方案。

如果我使用 - 符号列出

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

如果我不使用 - 符号列出

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-11-16 15:12:25

不确定这是否有帮助,但是当我尝试加载第一个示例时,它对我有用。也许缩进不正确?

不管怎样,这在这里有效:

require "YAML"

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

哦,是的,让我添加适合我的 admin.yaml :

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
一刻暧昧 2024-11-16 15:12:25

如果您在生成 YAML 时遇到问题,我会尝试在控制台中构建一个对象,然后将其转换为 YAML 以查看其外观。例如:

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" 

如果它使您的生活更轻松,您甚至可以将其输出到文件中:

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

其结果:

--- 
: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文