问题描述
我有一张表,其中的数据如图所示
@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
也不应重复。在这种情况下,只有6
和resource
应该在左侧,而相应的选项作为下拉列表在右侧。我将如何在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_records
和Class_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