如何在Rails和haml中创建多个选择

问题描述

我有一张表,其中的数据如图所示

 @brief VNRecognizeTextRequestRevision1 only supports English

 @brief VNRecognizeTextRequestRevision2 supports English,Chinese,Portuguese,French,Italian,German and Spanish in the accurate 
recognition level. The fast recognition level supports English,German and Spanish. Best practice 
is to use supportedRecognitionLanguagesForTextRecognitionLevel 
to check for supported languages. As the underlying engine has 
changed from VNRecognizeTextRequestRevision1,results can differ 
but are generally more accurate. 

我希望创建一个haml选择,以便将id | Resource | Access ___________________________ 1 | Fee records | can_read 2 | Fee records | no_access 3 | Fee records | can_write 4 | Class register | can_read 5 | Class register | no_access 6 | Class register | can_write 放在左侧,将resource作为选择的选项。像

access

当用户选择Fee records -> [can_read,no_access,can_write] Class register -> [can_read,can_write] 下选项can_read我希望找回ID为Fee Records从上面表时1 {} {1}中选择选项下,我希望获得ID no_access。所以基本上我将有两个选择,但是选择的选项必须返回上表中的ID。 Class register也不应重复。在这种情况下,只有6resource应该在左侧,而相应的选项作为下拉列表在右侧。我将如何在Ruby on Rails和haml上实现这一目标?

解决方法

这就是我为解决上述问题所做的事情。在控制器的新方法中,我有

def new
 @permissions = ["no_access","can_read","can_write"]
 @unique_permissions = Model.select(:resource).map(&:resource).uniq
end

@unique_permissions = Model.select(:resource).map(&:resource).uniq行帮助我从上表中获得唯一值,因此我将收集[Fee_records,Class_register]

在haml中,我将拥有

- @unique_permissions.each do |permission|
      .row
        .col-md-4
          = permission
        = f.select permission,@permissions,{},class: "form-control col-md-8"
  

这将给我两个选择,@permissions包含["no_access","can_write"]的数组作为每个选择的选项。 在控制器中,我将允许参数为Fee_recordsClass_register 在创建动作中,我将从参数中获取permission_keys,基本上是将[Fee_records,Class_register]作为键。 要获取与用户选择的选项相关的特定ID,我将遍历

之类的键
permission_keys.each do |permission|
  permission_id = Model.find_by(
    resource: permission,access: permission_params[:permission]
   ).id
  # other code
end

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...