Forum: Ruby on Rails Create checkboxes on page using contents of a hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Hunter W. (Guest)
on 2006-05-10 03:44
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">

<head>

</head>

<body>

Some text here


</br>


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

</body>

</html>
Chris T (Guest)
on 2006-05-10 13:37
(Received via mailing list)
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
Hunter W. (Guest)
on 2006-05-10 19:21
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!
Chris T (Guest)
on 2006-05-10 21:40
(Received via mailing list)
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,...
Hunter W. (Guest)
on 2006-05-11 03:55
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') %> </br>

<%= 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: </body>
26: </html>

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"}
Adam B. (Guest)
on 2006-05-11 04:36
><%= value %> <%= check_box('Site', key, {:checked => 'checked'},
>'yes') %> </br>

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

-Adam
Hunter W. (Guest)
on 2006-05-11 07:49
Adam B. wrote:
>><%= value %> <%= check_box('Site', key, {:checked => 'checked'},
>>'yes') %> </br>
>
> You're missing a <% end %> after this line.
>
> -Adam

You are correct, sir.

Thanks to both of you for the help!  I appreciate it!
This topic is locked and can not be replied to.