Forum: Ruby Rails - Hashes

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.
074597b4e64c139d5e1d51c52e475de2?d=identicon&s=25 Paul D. Kraus (Guest)
on 2006-05-27 08:02
(Received via mailing list)
I am trying to iterate over a hash but it only displays '#' for my key.
I know this is not the case because it outputs the correct number of
lines
based on how many keys I have. If it were trully '#' it would only print
one
line.

Here is the code that is producing the output...
  <% @payment.batch.each do|key,value| %>
    <li><%=key%> -> <%=value%></li>
  <% end %>

here is my output ...

   - # -> 1
   - # -> 1



Here is the code that builds the hash...
Controller...
def add_invoice
    @payment = find_payment
    @invoice = Invoice.find(params[:id])
    @payment.add_invoice(@invoice)
  end
def find_payment
    session[:payment] ||= Payment.new
  end

and the class....
class Payment
  include Reloadable
  attr_reader :batch

  def initialize
    @batch = {}
  end

  def add_invoice(invoice)
    unless @batch[invoice]
      @batch[invoice] = 1
    end
  end
end


TIA,
Paul
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-05-27 08:11
(Received via mailing list)
Paul D. Kraus wrote:
>
> here is my output ...
>
>   - # -> 1
>   - # -> 1

What does view|source on the resulting Web page show you?

I'm betting key is an object with no useful default to_s.



--
James Britt

"In physics the truth is rarely perfectly clear, and that is certainly
  universally the case in human affairs. Hence, what is not surrounded
by
  uncertainty cannot be the truth."
  - R. Feynman
074597b4e64c139d5e1d51c52e475de2?d=identicon&s=25 Paul D. Kraus (Guest)
on 2006-05-27 08:20
(Received via mailing list)
>
>
> What does view|source on the resulting Web page show you?
>
> I'm betting key is an object with no useful default to_s.
>

Good Call ..

 <li>#<Invoice:0xb76e54d8> -> 1</li>

Ok so why is the key getting set to this.

    @invoice = Invoice.find(params[:id])
    @payment.add_invoice(@invoice)

are the lines that get the "key" params[:id] is just the id number from
a
row in result set. Or more accuratly its just this from the url...
http://xxx.xxx.xxx.xxx:3000/selection/add_invoice/4

This is my very first attempt at rails and ruby is pretty new to me as
well.
but shouldn't params[:id] = '4'

Paul
074597b4e64c139d5e1d51c52e475de2?d=identicon&s=25 Paul D. Kraus (Guest)
on 2006-05-27 08:20
(Received via mailing list)
>
>
> This is my very first attempt at rails and ruby is pretty new to me as
> well.
> but shouldn't params[:id] = '4'
>

Never Mind = its getting "Invoice.find"

Thanks for the help.
This topic is locked and can not be replied to.