使用 ruby 文件中的数组在 haml 视图中创建单选按钮
Using an array in ruby file to create radio buttons in haml view
我正在为程序创建示例问题页面,但未能找到适合我需要的解决方案,如有任何帮助,我们将不胜感激。
在我的 survey_handler.rb 文件中,我有以下方法(示例的部分代码):
def temp_structure [
{ type: 'question', question_type: SurveyDefinition::QT_STRING, id: 1, editable: true, deletable: false, required: false, title: "String question", body: "with body" },
{ type: 'question', question_type: SurveyDefinition::QT_INTEGER, id: 3, editable: true, deletable: false, required: true, title: "Integer please" },
{ type: 'question', question_type: SurveyDefinition::QT_RADIO_BUTTONS, id: 7, editable: true, deletable: false, required: true, title: "Please choose one", choices: ["Yes", "No", "Don't Know"], },
在我的 haml 视图中,我有:
- case question['question_type']
- when SurveyDefinition::QT_STRING
= f.label :data_string, survey_question_label(question['title'], question['body'], question['required']), class: "form-label"
= f.text_field :data_string, class: "form-control", required: question['required']
- when SurveyDefinition::QT_INTEGER
= f.label :data_integer, survey_question_label(question['title'], question['body'], question['required']), class: "form-label"
= f.number_field :data_integer, class: "form-control", required: question['required']
我一直在努力实现一个标准的 ruby 方法,我在 QT_RADIO_BUTTONS 中遍历 'choices' 数组来创建每个单选按钮,但我已经 运行 一个又一个错误,最好的写法是什么?
(ruby 和 haml 相对较新,也是第一个问题,所以任何关于我的问题结构的反馈都会很好)
编辑:这是我尝试过的最新方法,但我不确定如何指向 'choices' 数组
- when SurveyDefinition::QT_RADIO_BUTTONS
.row.form-group
= f.label :data_string, survey_question_label(question['title'], question['body'], question['required']), class: "form-label"
= @choices.each do |choice|
.col-1
= f.radio_button :data_string, "#{choice}"
据我了解,您需要 choices: ["Yes", "No", "Don't Know"]
。所以要在视图中使用:
- question['choices'].each do |choice|
.col-1
= f.radio_button :data_string, choice
此外,不需要使用字符串插值#{choice}
,因为选项已经是字符串。
我正在为程序创建示例问题页面,但未能找到适合我需要的解决方案,如有任何帮助,我们将不胜感激。
在我的 survey_handler.rb 文件中,我有以下方法(示例的部分代码):
def temp_structure [
{ type: 'question', question_type: SurveyDefinition::QT_STRING, id: 1, editable: true, deletable: false, required: false, title: "String question", body: "with body" },
{ type: 'question', question_type: SurveyDefinition::QT_INTEGER, id: 3, editable: true, deletable: false, required: true, title: "Integer please" },
{ type: 'question', question_type: SurveyDefinition::QT_RADIO_BUTTONS, id: 7, editable: true, deletable: false, required: true, title: "Please choose one", choices: ["Yes", "No", "Don't Know"], },
在我的 haml 视图中,我有:
- case question['question_type']
- when SurveyDefinition::QT_STRING
= f.label :data_string, survey_question_label(question['title'], question['body'], question['required']), class: "form-label"
= f.text_field :data_string, class: "form-control", required: question['required']
- when SurveyDefinition::QT_INTEGER
= f.label :data_integer, survey_question_label(question['title'], question['body'], question['required']), class: "form-label"
= f.number_field :data_integer, class: "form-control", required: question['required']
我一直在努力实现一个标准的 ruby 方法,我在 QT_RADIO_BUTTONS 中遍历 'choices' 数组来创建每个单选按钮,但我已经 运行 一个又一个错误,最好的写法是什么?
(ruby 和 haml 相对较新,也是第一个问题,所以任何关于我的问题结构的反馈都会很好)
编辑:这是我尝试过的最新方法,但我不确定如何指向 'choices' 数组
- when SurveyDefinition::QT_RADIO_BUTTONS
.row.form-group
= f.label :data_string, survey_question_label(question['title'], question['body'], question['required']), class: "form-label"
= @choices.each do |choice|
.col-1
= f.radio_button :data_string, "#{choice}"
据我了解,您需要 choices: ["Yes", "No", "Don't Know"]
。所以要在视图中使用:
- question['choices'].each do |choice|
.col-1
= f.radio_button :data_string, choice
此外,不需要使用字符串插值#{choice}
,因为选项已经是字符串。