# Create checkboxes on page using contents of a hash

**URL:** <https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086>\
**Category:** Rails\
**Created:** [May 10, 2006, 1:44am UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086 "2006-05-10T01:44:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 10, 2006, 1:44am UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/1 "2006-05-10T01:44:27Z")

</div>

Hi all,

I have a hash that is created in the index method of my controller.  
After it is created, the index.rhtml page is rendered. Upon this  
rendering I would like to create a check box for each key/value in the  
hash.

The has is @recordsHash

I have been trying varying stratagies like the one below, but to no  
avail. Do you have any ideas? Thanks for your help on this!

html lang='en"\>

Some text here

\<%= form\_tag(:controller =\> “AddMassMail”, :action =\> ‘addmassmail’) %\>

```
   <% @recordsHash.each do {|key, value| %>
       <%= "Site #{value}<%= check_box('Site', '#{key}', {:checked 

```

=\> ‘checked’}, ‘yes’) %\> \<% } %\>

\<%= submit\_tag(“Add Mass Mailing”) %\>

\<%= end\_form\_tag %\>

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 10, 2006, 11:37am UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/2 "2006-05-10T11:37:16Z")

</div>

Hunter W. wrote:

> Hi all,
> 
> I have a hash that is created in the index method of my controller.  
> After it is created, the index.rhtml page is rendered. Upon this  
> rendering I would like to create a check box for each key/value in the  
> hash.

Think you have an error in your each iteration. Believe the correct  
syntax is:

some\_hash.each { |key, value| #something here }

or

some\_enumerable.each do |key, value|  
#something here  
end

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 10, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/3 "2006-05-10T17:21:34Z")

</div>

Chris T wrote:

> Hunter W. wrote:
> 
> > Hi all,
> > 
> > I have a hash that is created in the index method of my controller.  
> > After it is created, the index.rhtml page is rendered. Upon this  
> > rendering I would like to create a check box for each key/value in the  
> > hash.
> 
> Think you have an error in your each iteration. Believe the correct  
> syntax is:
> 
> some\_hash.each { |key, value| #something here }
> 
> or
> 
> some\_enumerable.each do |key, value|  
> #something here  
> end  
> Hi Chris,

I tried both of those but I am still getting syntax errors:

\<% @recordsHash.each do |key, value| %\>

```
   <%= "Site #{value}<%= check_box('Site', '#{key}', {:checked => 

```

‘checked’}, ‘yes’) %\>

I may have to create true objects. This is just an API calling app, so  
I was hoping to keep it simple, but I can’t get this last part working.  
Bummer!

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 10, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/4 "2006-05-10T19:40:17Z")

</div>

Hunter W. wrote:

> > > 
> > 
> > some\_enumerable.each do |key, value|  
> > ‘checked’}, ‘yes’) %\>
> 
> I may have to create true objects. This is just an API calling app, so  
> I was hoping to keep it simple, but I can’t get this last part working.  
> Bummer!

What exactly is the syntax error? Maybe an email typo but think you’re  
also missing a %\> after #{value} and I thought you needed to use double  
quotes for ruby to see the #{} construction, so it would need to be:

\<%= check\_box(‘Site’, “#{key}”,…

though would have thought you could skip the quotes and #{} completely:  
\<%= check\_box(‘Site’, key,…

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 11, 2006, 1:55am UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/5 "2006-05-11T01:55:58Z")

</div>

I agree, but I am still getting an unhelpful syntax error from Rails

Here is my code:

\<%= form\_tag(:controller =\> “AddMassMail”, :action =\> ‘addmassmail’) %\>

```
   <% @recordsHash.each do |key, value| %>

   <%= value %> <%= check_box('Site', key, {:checked => 'checked'}, 

```

‘yes’) %\>

\<%= submit\_tag(“Add Mass Mailing”) %\>

\<%= end\_form\_tag %\>

Here is the error. Thank you for your continued help.

SyntaxError in Add\_mass\_mail#index

Showing app/views/add\_mass\_mail/index.rhtml where line #27 raised:

compile error  
./script/…/config/…/app/views/add\_mass\_mail/index.rhtml:27: syntax  
error

Extracted source (around line #27):

24:  
25:   
26:

Trace of template inclusion: /app/views/add\_mass\_mail/index.rhtml

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

#{RAILS\_ROOT}/app/views/add\_mass\_mail/index.rhtml:27:in `module\_eval’

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_view/base.rb:496:in  
`module_eval' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_view/base.rb:496:in`compile\_template’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_view/base.rb:300:in  
`compile_and_render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_view/base.rb:285:in`render\_template’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_view/base.rb:244:in  
`render_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/base.rb:697:in`render\_file’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/base.rb:619:in  
`render_with_no_layout' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/layout.rb:251:in`render\_without\_benchmark’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/benchmarking.rb:53:in  
`render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/benchmarking.rb:53:in`measure’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/benchmarking.rb:53:in  
`render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/base.rb:909:in`perform\_action\_without\_filters’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/filters.rb:355:in  
`perform_action_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/benchmarking.rb:69:in`perform\_action\_without\_rescue’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/benchmarking.rb:69:in  
`measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/benchmarking.rb:69:in`perform\_action\_without\_rescue’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/rescue.rb:82:in  
`perform_action' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/base.rb:379:in`send’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/base.rb:379:in  
`process_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/filters.rb:364:in`process\_without\_session\_management\_support’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/session\_management.rb:117:in  
`process' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/dispatcher.rb:38:in`dispatch’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick\_server.rb:115:in  
`handle_dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:81:in`service’  
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in`run’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in`start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in`start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in`start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in`start’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick\_server.rb:67:in  
`dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/servers/webrick.rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in`require\_\_’  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:136:in`require’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/server.rb:30  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in`require’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active\_support/dependencies.rb:136:in  
`require’  
script/server:3

#{RAILS\_ROOT}/app/views/add\_mass\_mail/index.rhtml:27:in `module_eval' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_view/base.rb:496:in`module\_eval’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_view/base.rb:496:in  
`compile_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_view/base.rb:300:in`compile\_and\_render\_template’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_view/base.rb:285:in  
`render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_view/base.rb:244:in`render\_file’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/base.rb:697:in  
`render_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/base.rb:619:in`render\_with\_no\_layout’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/layout.rb:251:in  
`render_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/benchmarking.rb:53:in`render’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/benchmarking.rb:53:in  
`measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/benchmarking.rb:53:in`render’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/base.rb:909:in  
`perform_action_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/filters.rb:355:in`perform\_action\_without\_benchmark’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/benchmarking.rb:69:in`measure’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/rescue.rb:82:in`perform\_action’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/base.rb:379:in  
`send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/base.rb:379:in`process\_without\_filters’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action\_controller/filters.rb:364:in  
`process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/session_management.rb:117:in`process’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/dispatcher.rb:38:in  
`dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:115:in`handle\_dispatch’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick\_server.rb:81:in  
`service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in`start\_thread’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in`start\_thread’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in`each’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in`start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:67:in`dispatch’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/servers/webrick.rb:59  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in`require’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active\_support/dependencies.rb:136:in  
`require' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/server.rb:30 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in`require\_\_’  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:136:in`require’  
script/server:3

Request

Parameters: None

Show session dump

* * *

flash: !ruby/hash:ActionController::Flash::FlashHash {}

Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 11, 2006, 2:36am UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/6 "2006-05-11T02:36:48Z")

</div>

> \<%= value %\> \<%= check\_box(‘Site’, key, {:checked =\> ‘checked’},  
> ‘yes’) %\>

You’re missing a \<% end %\> after this line.

-Adam

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [May 11, 2006, 5:49am UTC](https://www.ruby-forum.com/t/create-checkboxes-on-page-using-contents-of-a-hash/60086/7 "2006-05-11T05:49:50Z")

</div>

Adam B. wrote:

> > \<%= value %\> \<%= check\_box(‘Site’, key, {:checked =\> ‘checked’},  
> > ‘yes’) %\>
> 
> You’re missing a \<% end %\> after this line.
> 
> -Adam

You are correct, sir.

Thanks to both of you for the help! I appreciate it!
