HAML: form_tag dilemma (indentation?)

走远了吗. 提交于 2020-01-01 03:59:27

问题


I'm making "user settings form", and stuck with HAML:

= form_tag('/')
  - [1,2,3].each do |i|
    =  check_box_tag "accept#{i}"
  = submit_tag

This results in "syntax error, unexpected kENSURE, expecting $end".

The working variant is

= form_tag('/')
  - [1,2,3].each do |i|
  =  check_box_tag "accept#{i}"
  = submit_tag

Results in

 <input id="accept1" name="accept1" type="checkbox" value="1" />
    <input name="commit" type="submit" value="Save changes" />
    <input id="accept2" name="accept2" type="checkbox" value="1" />
    <input name="commit" type="submit" value="Save changes" />
    <input id="accept3" name="accept3" type="checkbox" value="1" />
    <input name="commit" type="submit" value="Save changes" />
    <form action="/" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="tuniieYp9hgvGBMaYDjK2l3Mgjpxo9F1LPzqEF4ykHg=" /></div>

Wrong: controls outside form tag, 3 times submit button.

Any ideas on fixing would be appreciated. Thank you!


回答1:


You should be passing a block to form_tag. I think you want:

Rails 2.x:

- form_tag('/') do
  - [1, 2, 3].each do |i|
    = check_box_tag "accept#{i}"
  = submit_tag

Rails 3+:

= form_tag('/') do
  - [1, 2, 3].each do |i|
    = check_box_tag "accept#{i}"
  = submit_tag


来源:https://stackoverflow.com/questions/3483592/haml-form-tag-dilemma-indentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!