この問題を何と呼ぶべきか正確にはわかりません。まだレールに慣れていません。
状況:オークションに多数のロットが含まれています。/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>
データベースにはロットが 1 つしかありません。余分なループ インスタンスがどこから来ているのかわからない。
これは、私が見ているオークションに関係なく、どのロットリストでも発生します。
また、
<%= @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
andvalidates
行です。
ページをヒットしたときのログがリクエストされたので、ここにあります。
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 の index メソッドで何かを設定する必要がありますが、何がわかりません。
どんな助けでも大歓迎です。