Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:13:32+00:00 2026-06-03T21:13:32+00:00

I have a form in rails 3.2 for creating users in a company. Each

  • 0

I have a form in rails 3.2 for creating users in a company. Each company has sites. A site is assigned to a user via a collection_select, and then each site has a number of departments which are selected using check boxes. A user belongs to a site and can belong to many departments from that site.

I need the department check box options to change dependant on the selected value in the sites collection_select.

I have been trying to do this by using :onchange of the collection_select to call a partial containing the departments. I’ve managed to get this to work but only displaying all departments for the company. Not departments for each individual site. I don’t understand how to pass the selected site to the partial so it can return the correct departments (if this is even possible).

My code so far is:

Form:

    <%= form_for @user, :url => front_admin_users_path do |f| %>
      <%= devise_error_messages! %>

      <div><%= f.label :email %><br />
      <%= f.email_field :email %></div>

      <%= f.collection_select :role, User::ROLES, :to_s, :humanize %>

      <%= f.collection_select :site_id, current_user.company.sites.all, :id, :name, {}, :onchange => "jQuery.get('/load_department')",  :remote => true  %>

      <div id="department_frame"></div> 

      <div><%= f.label :password %><br />
      <%= f.password_field :password %></div>

      <div><%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation %></div>

      <div><%= f.submit "Sign up" %></div>
    <% end %> 

users_controller.rb:

     def load_department
       respond_to do | format |  
         format.js {render :layout => false}  
       end
     end

load_department.js.erb:

    $("#department_frame").html( "<%=j render(:partial =>"department") %>" );

_department.html.erb:

    <% current_user.company.sites.each do |site| %>
<% for department in site.departments %>
    <%= check_box_tag "user[department_ids][]", department.id, current_user.department_ids.include?(department.id) %>
    <%= department.name %>
<% end %>

I have googled for days trying to find the solution but can’t find exactly what I need. The nearest thing I’ve found is Ryan Bate’s dynamic collection selects revised using the grouped_collection_select method (which I’ve used in another part of the project), but this doesn’t seem to deal with select boxes. Any help would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:13:34+00:00Added an answer on June 3, 2026 at 9:13 pm

    You could try passing the site_id as a parameter in the URL to the AJAX call and then read that value off in the controller. In the controller method, you can also then get the list of departments for that particular site and then pass that off to the view that is being rendered.

    I have edited the relevant parts of the code to explain what I mean better. Hopefully this makes sense 🙂

    Form

    <%= f.collection_select :site_id, current_user.company.sites.all, :id, :name, {}, :onchange => "jQuery.get('/load_department?site=' + $('#user_site_id').val())",  :remote => true  %>
    

    users_controller.rb:

    def load_department
        site = params[:site]
        @departments = Site.find(site).departments
    
        respond_to do | format |  
            format.js {render :layout => false}  
        end
    end
    

    _department.html.erb:

        <% for department in @departments %>
            <%= check_box_tag "user[department_ids][]", department.id, current_user.department_ids.include?(department.id) %>
            <%= department.name %>
        <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Specifically, I have a form for creating a User . One of the fields
I have a form where I have an administrator creating new users. The form
I'm creating a form in a Rails 3 app that has a dropdown field.
I have a form for creating a new user. I'd like to pass a
I have a Rails form that is being used for creating and editing a
I would like to have Ajax form in Rails so i'm using form_remote_tag. The
I have a rails form with a datetime_select field. When I try to submit
I have a Rails form for email, and I would like to have auto-completion
I have a simple Ruby on Rails form which includes an authenticity_token. Unfortunatly, I
I have a live rails website and I want to have a form with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.