HAML: form_tag dilemma (indentation?)

Alexander picture Alexander · Aug 14, 2010 · Viewed 17k times · Source

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!

Answer

nirvdrum picture nirvdrum · Aug 14, 2010

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