我当前正在尝试显示第一条记录。当有人决定购买葡萄酒时,记录会更新。我如何获得特定葡萄酒的第一笔记录?
我想使用.find(1)吗?但是如何在我的控制器中实现呢?
我当前的控制器:
def create
wine = Wine.find(params[:wine_id])
if current_user == wine.user
flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
else
start_date = Date.parse(reservation_params[:start_date])
@reservation = current_user.reservations.build(reservation_params)
@reservation.wine = wine
@reservation.price = wine.price
@reservation.in_stock = wine.in_stock - @reservation.bottle
@reservation.total = wine.price * @reservation.bottle
wine.update(in_stock: @reservation.in_stock)
# @reservation.save
if @reservation.save
if wine.Reservieren?
flash[:notice] = "Reserviert!"
else
@reservation.Versendet!
flash[:notice] = "Eroflgreich bestellt!"
end
else
flash[:alert] = "Can't make a reservation!"
end
end
redirect_to wine
end
我当前的视图:
<% @wines.each do |wine| %>
<div class="row">
<div class="col-md-3">
<%= link_to wine.wine_name, wine_path(wine) %>
</div>
<div class="col-md-3">
<%= wine.in_stock %> <!-- Display first record -->
</div>
<div class="col-md-3">
<% if !wine.in_stock.blank? %>
Aktiv
<% else %>
Nicht Aktiv
<% end %>
</div>
<div class="col-md-3 right">
<%= link_to "Update", details_wine_path(wine), class: "btn btn-form" %>
</div>
</div><!-- row -->
<hr/>
<% end %>
答案 0 :(得分:0)
也许您可以解释一些更好的代码,因为在您的视图中您收到@wines
,并且它不是来自您发布的此控制器操作。
但是,如果您想进行首次注册,则可以基本上调用wines.first
。
例如,您将从<%= wine.in_stock %>
更改为<%= @wines.first %>
。
以下是一些与线程相关的内容: Display first record in rails