Jquery Rails 4动态集合\u选择

Jquery Rails 4动态集合\u选择,jquery,ajax,ruby-on-rails-4,collection-select,Jquery,Ajax,Ruby On Rails 4,Collection Select,这似乎是一个非常流行的问题,尽管我还没有找到适合我的教程或线程。我在表单中有两个下拉菜单,团队类型和用户角色,其中用户角色取决于团队类型。团队类型的选项作为数组存储在模型中,因为只有5个选项(艺术家、场地、推广人、独立者、其他)。我想做的是从模型中获取用户角色的选择,并根据团队类型选择适当的数组。这是可能的,还是我需要为每个团队类型创建模型并将ID传递给联接表以选择适当的用户角色?多谢各位 型号 class WaitingList < ActiveRecord::Base COMPAN

这似乎是一个非常流行的问题,尽管我还没有找到适合我的教程或线程。我在表单中有两个下拉菜单,团队类型和用户角色,其中用户角色取决于团队类型。团队类型的选项作为数组存储在模型中,因为只有5个选项(艺术家、场地、推广人、独立者、其他)。我想做的是从模型中获取用户角色的选择,并根据团队类型选择适当的数组。这是可能的,还是我需要为每个团队类型创建模型并将ID传递给联接表以选择适当的用户角色?多谢各位

型号

class WaitingList < ActiveRecord::Base
  COMPANIES = ['—Select—', 'Artist Team', 'Venue Team', 'Promoter', 'Independent', 'Other']
  ARTIST_TEAM = ['-Select-', 'Artist', 'Manager', 'Tour Manager', 'Production Manager', 'Agent', 'Other']
  VENUE_TEAM = ['-Select-', 'Artist Liason', 'Stage Manager', 'Production Manager', 'Owner', 'Other']
  PROMOTER = ['-Select', 'Talent Buyer', 'Other']
  INDEPENDENT = ['-Select', 'Agent', 'Photo/Video', 'Tour Manager', 'Manager', 'Other']
end 
class WaitingList
表格

<div class="form--col">
  <label>Team Type</label>
    <div class="dropdown-wrapper">
      <%= f.collection_select :company_type, WaitingList::COMPANIES, :to_s, :to_s, {:include_blank => false}, {:class => "form--dropdown -team_type"} %>
    </div>
</div>

<div class="form--col -inactive">
  <label>Main Role</label>
    <div class="dropdown-wrapper">
      <%= f.collection_select :user_type, WaitingList::USERS, :to_s, :to_s, {:include_blank => false}, {:class => "form--dropdown", :disabled => "disabled"} %>
    </div>
</div>
<div class="form--col">
  <label>Team Type</label>
  <div class="dropdown-wrapper">
    <%= f.collection_select :company_type, WaitingList::COMPANIES, :to_s, :to_s, {include_blank: false}, {onchange: "getRoles();", class: "form--dropdown -team_type"} %>
  </div>
</div>

<div class="form--col -inactive">
  <label>Main Role</label>
  <div class="dropdown-wrapper">
    <%= f.collection_select :user_type, {}, {prompt: 'Main Role'} {:class => "form--dropdown", :disabled => "disabled"} %>
  </div>
</div>

团队类型
false},{:class=>“表单--下拉列表-团队类型”}%>
主要角色
false},{:class=>“form--dropdown”,:disabled=>“disabled”}%>

我认为您应该通过添加javascript
onChange
函数来满足ajax请求。还添加了一个新方法来处理此ajax请求

表格

<div class="form--col">
  <label>Team Type</label>
    <div class="dropdown-wrapper">
      <%= f.collection_select :company_type, WaitingList::COMPANIES, :to_s, :to_s, {:include_blank => false}, {:class => "form--dropdown -team_type"} %>
    </div>
</div>

<div class="form--col -inactive">
  <label>Main Role</label>
    <div class="dropdown-wrapper">
      <%= f.collection_select :user_type, WaitingList::USERS, :to_s, :to_s, {:include_blank => false}, {:class => "form--dropdown", :disabled => "disabled"} %>
    </div>
</div>
<div class="form--col">
  <label>Team Type</label>
  <div class="dropdown-wrapper">
    <%= f.collection_select :company_type, WaitingList::COMPANIES, :to_s, :to_s, {include_blank: false}, {onchange: "getRoles();", class: "form--dropdown -team_type"} %>
  </div>
</div>

<div class="form--col -inactive">
  <label>Main Role</label>
  <div class="dropdown-wrapper">
    <%= f.collection_select :user_type, {}, {prompt: 'Main Role'} {:class => "form--dropdown", :disabled => "disabled"} %>
  </div>
</div>
路线

添加了等待列表控制器的路由

  def get_role()
    if params[:role]
      case params[:role]
      when 'Artist Team'  
        roles = WaitingList::ARTIST_TEAM
      when 'Venue Team'
        roles = WaitingList::VENUE_TEAM
      when 'Promoter'
        roles = WaitingList::PROMOTER
      when 'Independent'
        roles = WaitingList::INDEPENDENT
      when 'Others'
        roles = []
      end      
      render json: {roles: roles}
    end 
  end
resources(:waiting_lists) do
  collection do
    get(':role/get_role', action: :get_role)
  end
end

希望这对您有所帮助。

谢谢!这正是我想要的。我在JS函数的第一行“解析错误-意外的令牌getRoles”中遇到了一个错误,知道这是怎么回事吗?必须添加var roles=data.roles;但是现在它工作得很好,谢谢!