<br><br><div><span class="gmail_quote">On 5/27/07, <b class="gmail_sendername">John Winters</b> <<a href="mailto:john@sinodun.org.uk">john@sinodun.org.uk</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello there,<br><br>I'm fairly new to RoR and am working my way through various books.<br>Mainly "Agile Web Development with Rails" and "Rails Recipes".<br><br>One idea I've picked up from the Rails Recipes book is to use roles and
<br>rights to control what parts of the system users can access.  This<br>involves setting up a "roles" table in the database and then linking<br>role records to user records with has_and_belongs_to_many.  This I have
<br>working just fine, but it seems to me fairly obvious that I need to<br>provide an administration interface to allow users to be given and<br>deprived of roles.  Currently I can only update a user's roles by way of
<br>the console.  I picture something like this in the user edit page:<br><br>Owner         [x]<br>Administrator [ ]<br>Head of Dept  [x]<br>Teacher       [ ]<br>Pupil         [ ]<br><br>Note that in my system a user may have more than one role.
<br><br>The problem I then have is that the data item "user.roles" is an array<br>and not suitable for passing to the form helpers.  My approach to<br>solving this was to create a hash within the user record as it is
<br>loaded, using the name of each role as the key and having a boolean flag<br>indicating whether or not the user had that role.  Like this:<br><br>     @role_flags = {}<br>     roles = Role.find(:all)<br>     for role in roles
<br>       @role_flags [<a href="http://role.name">role.name</a>] = self.has_role(<a href="http://role.name">role.name</a>) ? 1 : 0<br>     end<br><br><br>This I thought would give me attributes which could be passed to the
<br>form helpers in RoR, but I can't discover how to do it.  There's an<br>obscure reference to accessing nested attributes on page 480 of the<br>Agile Web Development with Rails book, but it doesn't actually tell you
<br>how to do it.<br><br>Can anyone give me any pointers on how to do the above?  Am I going<br>about it in the wrong way?  Obviously it's simple enough to do if I<br>ignore Rails and do it on my own, but I can't help feeling I'm doing it
<br>wrong if I don't make use of the framework.<br><br>TIA,<br>John<br>_______________________________________________<br>chat mailing list<br><a href="mailto:chat@lrug.org">chat@lrug.org</a><br><a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org">
http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br></blockquote></div><br>A bit of a google came up with<br>
<br>
<a href="http://typo.railsstudio.com/articles/2006/03/31/multi-select-list-boxes-in-ruby-on-rails">http://typo.railsstudio.com/articles/2006/03/31/multi-select-list-boxes-in-ruby-on-rails</a><br>
<br>
which shows how to add appropriate options and an example of parsing the results from the form.<br>
For a slightly more complex recipie,<br><br><a href="http://www.telefonica.net/web2/bizarro/blog/2006/04/how-to-use-multi-select-list-boxes.html">http://www.telefonica.net/web2/bizarro/blog/2006/04/how-to-use-multi-select-list-boxes.html
</a><br><br>comes with it's own helper, but specifically addresses HABTM.<br><br>Dj<br><br>