SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help adding people to groups

    Hi all,

    i need some help trying to figure out how to add people to groups. I have models set up for my groups, and i have scaffolding set up for creating new groups, but i'm having problems adding people to each group. I have a model for poeple, but i'm not sure how to go about adding the forms for adding people to groups. I was thinking of adding a link on my page to view the group, so i would think i'd need to pass the id of the group to the form for adding a new person to the group.

    At this point i'm absolutely lost, and i've been scaning lenz's book and the agile book for examples on how to do what i'm trying to do, but i can't seem to find anything.

    can any of you more experienced rails developers offer any suggestions?


    Thanks,
    john

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how I've done it. I'm sure the coding can be improved - I tend to write fairly verbose code. However, this will at least get you started. I've called my groups of people 'crowds':

    Controller
    crowds_controller.rb
    Code:
      def assign_users
        @crowd = Crowd.find(params[:id])
        @all_users = User.find(:all)
        if !request.get? and params["crowds"]["user"]
          if @user = User.find(:first, :conditions => ["id = ?", params["crowds"]["user"]])
            if @crowd.users.include?(@user)
              flash[:notice] = "#{@user.user_name} is already assinged to crowd #{@crowd.name}"
            else
              @crowd.users << @user
              @crowd.save
              flash[:notice] = "Success. #{@user.user_name} assigned to crowd #{@crowd.name}"
            end
          end
        end
      end
    
      def unassign_user
        @crowd = Crowd.find(params[:id])
        @user = User.find(:first, :conditions => ["id = ?", params["user"]])
        if @crowd.users.delete(@user)
          @crowd.save
          flash[:notice] = "Success. #{@user.user_name} unassigned from crowd #{@crowd.name}"
        else
          flash[:notice] = "Failue. Unable to unassign #{@user.user_name} from crowd #{@crowd.name}"
        end
        redirect_to :action => 'assign_users', :id => @crowd
      end
    View
    assign_users.rhtml
    Code:
    <h2>crowd is <&#37;= @crowd.name -%></h2>
    
    <%= start_form_tag :action => 'assign_users', :id => @crowd %>
      <p><label for="user_crowd_ids">Users</label><br/>
      <%= collection_select("crowds", "user", @all_users, :id, :user_name, {:include_blank => true}) %> </p>
      <%= submit_tag 'Add' %>
    <%= end_form_tag %>
    
    <p><%= link_to "crowd list", :action => 'list' -%><p>
    
    <%   if @crowd.users.length > 0 -%>
    <h2>Users assigned to crowd <%= @crowd.name -%></h2>
    <table>
      <tr>
        <th>User</th>
      <tr>
        <% for user in @crowd.users %>
      <tr>
        <td><%= user.user_name -%></td>
        <td>
          <%= start_form_tag :action => 'unassign_user', :id => @crowd, :user => user -%>
          <%= submit_tag 'Unassign' -%>
          <%= end_form_tag -%>
        </td>
      </tr>
        <% end -%>
    </table>
    <% end -%>
    It's a very simple form as I use it within the administration pages of an intranet, so it's very basic.

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet! thanks for the help! i have couple of questions though. how are you linking to assign_user.rhtml? do you have a link within the "show" crowd? if so, do you need to pass the id of the crowd to the link to the assign_user page?

    thanks,
    John

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a list view with this table in:
    Code:
    <table>
      <tr>
        <th>Name</th>
        <th>Assigned Users</th>
        <th>Action</th>
      </tr>
    <&#37; @crowds.each do |crowd| -%>
      <tr>
        <td><%= crowd.name -%></td>
        <td><%= crowd.users.length -%></td>
        <td><%= link_to "Assign Users", :action => "assign_users", :id => crowd -%></td>
      </tr>
    <% end -%>
    </table>

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. I ran the code you offered, and i have another question for you...you create a drop down list with users, and you have an "Add" button, so my question is are you creating a bunch of users, then populating the dropdown with every user in the db?

    Again thanks for the help!

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allotherstaken View Post
    i've been scaning lenz's book and the agile book
    I think perhaps you need to do more than scan those books!

    The select dropdown is created with collection_select.

    The key line is:
    Code:
    <&#37;= collection_select("crowds", "user", @all_users, :id, :user_name, {:include_blank => true}) %>
    @all_users contains an array of user object generated in the controller here:
    Code:
    @all_users = User.find(:all)

  7. #7
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, i've read through lenz's book once, and i'm half way through the agile book. what i meant was i was scanning through what i've read to see if i can find examples of what i'm trying to do.

    i understand what the code is doing. my question was are you creating users elsewhere using a different form, then populating the drop down, and adding them to each "crowd" that way?

    i appreciate the help, but perhaps you should do more that just "scan" my post.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •