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.
Bontina C. (Guest)
on 2007-02-15 14: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
Jason R. (Guest)
on 2007-02-15 15: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
Bill W. (Guest)
on 2007-02-15 15:51
(Received via mailing list)
Hi Bontina,

Bontina C. 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
Jimtron (Guest)
on 2007-02-16 03: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.