還不知道到底要怎麼稱呼這個問題。仍然是新的鐵路。的Rails 3:循環顯示更多的記錄比應該
情況:一個拍賣包含很多地段。
我展示像/拍賣/ 3 /手/一個url拍賣的地段。
查看:
<ul>
<% @lots.each do |lot| %>
<li><%= lot.auction_id %>: <%= lot.id %></li>
<% end %>
</ul>
輸出這樣的:
<ul>
<li>3: 1</li>
<li>3: </li>
</ul>
我只有一個很多在我的數據庫。不確定額外的循環實例來自哪裏。
這發生在任何很多房源無論哪個拍賣我看。
此外,
<%= @lots.length %>
顯示2
<%= @lots.size %>
顯示2
<%= @lots.count %>
顯示1
我lots_controller看起來是這樣的:
def index
@auction = Auction.find(params[:auction_id])
@lots = @auction.lots
end
def create
@auction = Auction.find(params[:auction_id])
@lot = @auction.lots.build(params[:lot])
if @lot.save
redirect_to auction_lots_path, :notice => 'Lot was successfully created.'
else
render :action => "index"
end
end
我的模型:
class Auction < ActiveRecord::Base
...
has_many :lots
end
class Lot < ActiveRecord::Base
belongs_to :auction
...
end
的...
s爲只是attr_accesssible
和validates
線。
我擊球的時候被請求的頁面的日誌,在這兒呢。
Started GET "/auctions/8/lots" for 127.0.0.1 at 2013-02-13 16:35:51 -0500
Processing by LotsController#index as HTML
Parameters: {"auction_id"=>"8"}
Auction Load (0.1ms) SELECT "auctions".* FROM "auctions" WHERE "auctions"."id" = ? LIMIT 1 [["id", "8"]]
Lot Load (0.2ms) SELECT "lots".* FROM "lots" WHERE "lots"."auction_id" = 8
[#<Lot id: 18, description: "1923 Morgan", lot_number: 1, auction_id: 8, created_at: "2013-02-13 17:20:04", updated_at: "2013-02-13 17:20:04">]
Rendered layouts/_messages.html.erb (0.1ms)
Lot Exists (0.2ms) SELECT 1 AS one FROM "lots" WHERE "lots"."auction_id" = 8 LIMIT 1
Rendered lots/index.html.erb within layouts/application (9.4ms)
Completed 200 OK in 21ms (Views: 17.8ms | ActiveRecord: 0.5ms)
更新:
有人提到,它看起來像我使用@auction.lots.build
地方。
是的,我是。我在同一頁面(索引)上有一個表格,可以在其中添加大量內容。
<%= form_for(@auction.lots.build, :url => auction_lots_path(@auction)) do |f| %>
...
<% end %>
更改@auction.lots.build
擺脫了額外的行,雖然現在我不能成功地創建批次。我不知道該怎麼做。我可能必須在lots_controller的索引方法中設置一些東西,但我不知道是什麼。
任何幫助表示讚賞。
你是如何設置'@ lots'? – catsby 2013-02-13 20:16:51
'@auction = Auction.find(params [:auction_id]) @lots = @ auctions.lots' – 2013-02-13 20:21:22
它看起來像拍賣有很多的連接,並沒有指定很多的ID。我希望看到很多與拍賣有關的代碼。 – Kazetsukai 2013-02-13 20:36:07