Forum: Ruby on Rails undefined method ' each_with_index'

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.
D51a24ae1017f53158d70b621494255d?d=identicon&s=25 Bontina Chen (abon)
on 2007-02-15 13:27
Hi,

I'm creating a new model bt
class BT < ActiveRecord::Base

  has_many :bt_items
  belongs_to :user


end

and gather BT in a controller like this

  @bts = BT.find_by_product_id(@product.id)

then render in a view like this

  <div id= "bt">
<%=render :partial => "bt/bt", :collection => @bts
%>
</div>


and I got the following error

undefined method `each_with_index' for #<BT:0x45ed70c>

Any setting I should add to the BT model to get the method work?

THanks
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-02-15 14:48
(Received via mailing list)
The line:

@bts = BT.find_by_product_id(@product.id)

is only returning a single BT. You're trying to render as if you had a
collection of BTs, thus:

@bts = BT.find_all_by_product_id(@product.id)

should fix your problem.

Jason
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-02-15 14:51
(Received via mailing list)
Hi Bontina,

Bontina Chen wrote:

>  @bts = BT.find_by_product_id(@product.id)
>
> <%=render :partial => "bt/bt", :collection => @bts %>
>
> undefined method `each_with_index' for #<BT:0x45ed70c>

Just change the find to:

@bts = BT.find_all_by_product_id(@product.id)

Best regards,
Bill
6ebedec9056fcd5ecff378068cf03f83?d=identicon&s=25 Jimtron (Guest)
on 2007-02-16 02:09
(Received via mailing list)
What are you trying to do within the 'bt/bt' partial? What are you
trying to iterate over?  If you simply want access to a BT object in
your partial you could try <%= render :partial => "bt/bt", :object =>
@bts %>

Jim
This topic is locked and can not be replied to.