Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAML: form_tag dilemma (indentation?)

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!

like image 891
Alexander Avatar asked Aug 14 '10 13:08

Alexander


1 Answers

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
like image 67
nirvdrum Avatar answered Sep 18 '22 09:09

nirvdrum