不完全确定如何称呼这个问题。对 Rails 还是很陌生。
情况:拍卖包含许多批次。
我在/auctions/3/lots/之类的 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 的索引方法中设置一些东西,但我不知道是什么。
任何帮助表示赞赏。