0

我试图弄清楚在选择另一个选择字段的选项后如何确定一个选择字段的选项范围。

在“板”选择字段中选择板后,只有属于该板的列表才会出现在“列表”选择字段中。

一个常见的例子是在注册表单上,当在“国家”选择字段中选择一个国家时,“州/省”选择字段仅显示该国家/地区的州/省。

协会

class User < ActiveRecord::Base
  has_many :boards
  has_many :cards, through: :boards
end

class Board < ActiveRecord::Base
  belongs_to :user
  has_many :cards
end

class List < ActiveRecord::Base
  belongs_to :board
  has_many :cards
end

class Card < ActiveRecord::Base
  belongs_to :board
end

表单中使用的实例变量

class CardsController < ApplicationController
def edit
  @card = current_user.cards.find(params[:id])
  @board = @card.board
  @lists = @board.lists
end
end

形式

<%= form_for @card do |f| %>
  ...
  #show the user's boards
  <%= f.collection_select(:board_id, @boards, :id, :name) %>

  #show the lists of a board
  <%= f.collection_select(:list_id, @lists, :id, :name) %>
  ...
<% end %>
4

0 回答 0