我有两个型号
class SurveyResponse
has_many :answers, :class_name => SurveyResponseAnswer.name
accepts_nested_attributes_for :answers
end
class SurveyResponseAnswer
belongs_to :survey_response
validates_presence_of :answer_text
end
Run Code Online (Sandbox Code Playgroud)
在我的嵌套表单中,如果验证失败,我会在屏幕上显示此错误:
"答案答案文字不能为空"
我使用rails I18n成功地定制了我的属性名称.它的行为并不像我期望的那样.下面的yml文件不会影响error_messages_for中打印属性名称的方式
en:
activerecord:
models:
survey_response:
answers: "Response"
Run Code Online (Sandbox Code Playgroud)
但是如果从脚本/控制台我尝试
SurveyResponse.human_attribute_name("答案")
我得到了"响应"的预期结果.
我想要做的是验证错误消息说:
"响应答案文本不能为空".我需要解决的任何想法?
我在angularjs中有一个自定义指令.基本上我想要发生的是用户将从选择框中选择一个值,并将值附加到数组.这会导致我的自定义指令被调用并在屏幕上呈现一个新元素.我希望指令生成的文本字段绑定到控制器的属性.
HTML
<device-list ng-repeat="device in devices" key="device.key" display-name="device.display_name" bind-prefix="descriptions"></device-list>
Run Code Online (Sandbox Code Playgroud)
指示
angular.module('device_list_tag', []).
directive('deviceList', function() {
return {
restrict: 'E',
require: '?ngModel',
scope: {
devices: '=',
key: '=',
displayName: '=',
bindPrefix: '@'
},
link: function(scope, element, attrs) {
var deviceListElement = $(element)
var containerDiv = $('<div>')
.addClass('row')
var labelTag = $('<label>').text(scope.displayName)
.addClass('span1')
var bindField = attrs.bindPrefix+'.'+scope.key
var textField = $('<input>')
.addClass('span3')
.attr('ng-model', bindField)
containerDiv.append(labelTag)
containerDiv.append(textField)
deviceListElement.append(containerDiv)
}
}
})
Run Code Online (Sandbox Code Playgroud)
调节器
function DevicesCtrl($scope) {
descriptions = {}
}
Run Code Online (Sandbox Code Playgroud)
似乎ng-model是指令范围的本地,我如何将其应用于父级?如果我在页面上有一堆文本字段,就像
<input ng-model="descriptions.test"/>
Run Code Online (Sandbox Code Playgroud)
它的工作原理除了选择框生成的字段外.