1

I get hash that contains user role, controller name and list of the controller actions this role can access to.

access = {
    'admin' => [ 'users'    => ['edit','delete'],
                 'messages' => ['show','update']
               ],
     'user' => [ 'index'    => ['index','sign-out'],
                 'messages' => ['show','index']
               ]
}

How can i check what access['admin']['users']['edit'] exists?

0

1 Answer 1

5
access['admin']['users'].include? 'edit'

However, this may be a problem: you're using ... => ['users'=>['edit','delete'],...] This will create an array with a hash inside. Example:

{'a'=>'b'} #=> {"a"=>"b"}
['a'=>'b'] #=> [{"a"=>"b"}]

So consider using this:

access = {
    'admin' => { 'users'    => ['edit','delete'],
                 'messages' => ['show','update']
               },
     'user' => { 'index'    => ['index','sign-out'],
                 'messages' => ['show','index']
               }
}
Sign up to request clarification or add additional context in comments.

5 Comments

@jtbandes thank you for reply, it's exact what i need) But access['admin']['users'].contains? 'edit'. says no method exists. I replace it with access['admin']['users'].grep(/edit/).size >0 Maybe exists more elegant way?
@Vladimir Tsukanov: Glad I could help! If this answered your question, you can click the checkmark on the left, to mark it as answered. Thanks :)
@jtbandes I edited my previous comment. Maybe you know the answer
@Valdimir: Use include? instead of contains? and don't forget to add the missing comma between the 'admin' and 'user' hashes inside access.
Ah my mistake, the method is indeed called include? and not contains?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.