我使用http://htmltohaml.com/来转换我的ERB并且存在某种问题。
转换前的工作ERB
<input name="shares" ng-model="shares" type="number" required>
<input name="name" ng-model="name" required>
转换后不使用HAML:
这是我的错误: 显示/Users/project/app/views/shared/_employee.html.haml第9行引出:
<h3><%= t("admin.labels.employee") %> <%= content_tag(:span, employee_counter) %></h3>
<%
def create_validation_rules(field_rules)
Hash[field_rules.map do |rule|
next ["rule-#{rule[:name]}", rule[:value]] if rule[:value]
next ["msg-#{rule[:name]}", rule[:msg]] if rule[:msg]
end] if field_rules
end
%>
<div class="control-group form-controls">
<%= f.label :first_name, t("labels.name") %>
<%= f.text_field :first_name, placeholder: t("labels.first_name"), class: 'js-employeeName', id: "merchant_employees_attributes_#{index}_first_name", name: "merchant[employees_attributes][#{index}][first_name]", autocomplete: "off", maxlength: 50, size: nil, data: create_validation_rules(validations[:first_name]) %>
<%= f.text_field :middle_name, placeholder: t("labels.middle_name"), id: "merchant_employees_attributes_#{index}_middle_name", name: "merchant[employees_attributes][#{index}][middle_name]", autocomplete: "off", maxlength: 100, size: nil %>
<%= f.text_field :last_name, placeholder: t("labels.last_name"), class: 'js-employeeName', id: "merchant_employees_attributes_#{index}_last_name", name: "merchant[employees_attributes][#{index}][last_name]", autocomplete: "off", maxlength: 50, size: nil, data: create_validation_rules(validations[:last_name]) %>
</div>
<div class="control-group">
<%= f.label :email, t("labels.email_address") %>
<%= f.text_field :email, id: "merchant_employees_attributes_#{index}_email", name: "merchant[employees_attributes][#{index}][email]", autocomplete: "off", maxlength: 250, size: nil, data: create_validation_rules(validations[:email]) %>
</div>
<div class="control-group">
<% if force_superuser_role %>
<%= f.hidden_field :superuser, value: "1" %>
<% else %>
<%= f.label :superuser, t("admin.labels.superuser") %>
<%= f.check_box :superuser, class: "superuser_checkbox", id: "merchant_employees_attributes_#{index}_superuser", name: "merchant[employees_attributes][#{index}][superuser]"%>
<% end %>
</div>
答案 0 :(得分:2)
正确处理此问题的方法是将方法定义为控制器中的帮助程序:
class MyController
helper_method :create_validation_rules
private
def create_validation_rules(field_rules)
Hash[field_rules.map do |rule|
next ["rule-#{rule[:name]}", rule[:value]] if rule[:value]
next ["msg-#{rule[:name]}", rule[:msg]] if rule[:msg]
end] if field_rules
end
end
这样您就可以在视图中调用create_validation_rules
,而无需在那里定义方法。请参阅In Rails, what exactly do helper and helper_method do?以了解帮助方法的确切含义及其工作原理。
但是,如果您只是必须在View代码中使用嵌入式方法,那么有一个解决方案:您可以简单地删除内联函数中的最后一个end
,并正确缩进函数的主体和循环。
在这种情况下,错误消息会解释所有内容:
你不需要在Haml中使用“ - end”。不缩进以关闭块
更新您的HAML代码,这有效:
%h3
= t("admin.labels.employee")
= content_tag(:span, employee_counter)
- def create_validation_rules(field_rules)
- Hash[field_rules.map do |rule|
- next ["rule-#{rule[:name]}", rule[:value]] if rule[:value]
- next ["msg-#{rule[:name]}", rule[:msg]] if rule[:msg]
- end] if field_rules
.control-group.form-controls
= f.label :first_name, t("labels.name")
= f.text_field :first_name, placeholder: t("labels.first_name"), class: 'js-employeeName', id: "merchant_employees_attributes_#{index}_first_name", name: "merchant[employees_attributes][#{index}][first_name]", autocomplete: "off", maxlength: 50, size: nil, data: create_validation_rules(validations[:first_name])
= f.text_field :middle_name, placeholder: t("labels.middle_name"), id: "merchant_employees_attributes_#{index}_middle_name", name: "merchant[employees_attributes][#{index}][middle_name]", autocomplete: "off", maxlength: 100, size: nil
= f.text_field :last_name, placeholder: t("labels.last_name"), class: 'js-employeeName', id: "merchant_employees_attributes_#{index}_last_name", name: "merchant[employees_attributes][#{index}][last_name]", autocomplete: "off", maxlength: 50, size: nil, data: create_validation_rules(validations[:last_name])
.control-group
= f.label :email, t("labels.email_address")
= f.text_field :email, id: "merchant_employees_attributes_#{index}_email", name: "merchant[employees_attributes][#{index}][email]", autocomplete: "off", maxlength: 250, size: nil, data: create_validation_rules(validations[:email])
.control-group
- if force_superuser_role
= f.hidden_field :superuser, value: "1"
- else
= f.label :superuser, t("admin.labels.superuser")
= f.check_box :superuser, class: "superuser_checkbox", id: "merchant_employees_attributes_#{index}_superuser", name: "merchant[employees_attributes][#{index}][superuser]"
由于ERB视图代码中嵌入的函数定义异常,http://htmltohaml.com没有很好地处理它,只是将整个方法重新调整为HAML输出。虽然这显然是不正确的,但纠正是一件简单的事情。
如果您是一名优秀的网民(并且您已经在使用该工具),您将向http://htmltohaml.com的开发人员报告此问题,以便他可以解释此类HAML生成问题。
答案 1 :(得分:1)
最佳选择是将create_validation_rules
移动到帮助程序中,或者至少将其设置为控制器中的helper_method
。
要将其保留在haml中 - 您必须对块使用haml语法:
- def create_validation_rules(field_rules)
- if field_rules
- Hash.[] field_rules.map do |rule|
- if rule[:value]
- next ["rule-#{rule[:name]}", rule[:value]]
- if rule[:msg]
- next ["msg-#{rule[:name]}", rule[:msg]]