2016-05-31 57 views
1

我用http://htmltohaml.com/这个来转换我的ERB,并且存在某种问题。从ERB迁移到HAML后出错

的转换

<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> 

转换后不工作HAML前工作ERB:

这是我的错误: 显示/Users/project/app/views/shared/_employee.html。 HAML在行#9提出:

%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 
- end 
.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]" 
+2

你为什么要在视图内定义一个方法?你在这里要求很大的麻烦。 –

+1

我可能会建议在其他地方定义该函数,例如在帮助程序中或在控制器中计算该值。 HAML和ERB并不适合这个目的。 –

+0

@ChaseGilliam的确,HAML很难在视图中定义一个函数_ because_函数不应该在视图中定义。 –

回答

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?以准确了解辅助方法是什么以及它是如何工作的。


但是,如果你只是必须在查看代码中使用嵌入式方法,有一个解决办法:你可以简单地消除inline函数的最后end,妥善缩进功能的身体,循环。

在这种情况下,该错误信息会解释一切:

You don't need to use "- end" in Haml. Un-indent to close a block

更新您的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的问题。

+0

谢谢!它的工作 – christian

+0

我没有足够的声望投票:)下一次。 – christian

+0

今天晚些时候我可以给你5票,当时我的选票会补充。:D –

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]]