我在做一個雜貨網站。現在我想向產品頁面顯示 outletproduct 詳細資訊,并且我創建了一個名為 outletproductform.html.erb 的部分檔案以放在產品展示頁面上。但它向我顯示了錯誤,這是我的代碼。
產品控制器
class ProductsController < ApplicationController
def category
@category = Category.find(params[:id])
end
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
@outlet_product = OutletProduct.find(params[:id])
end
def new
@product = Product.new
@category = Category.all
end
def create
@product = Product.new(product_params)
@category_id = Category.all
if @product.save
flash[:success] = "Succesful create!"
redirect_to @product
else
render 'new'
end
end
private
def product_params
params.require(:product).permit(:name, :quantity, :price,
:category_id, :cost)
end
end
顯示.html.erb
<% provide(:title, @product.name)%>
<%= render @product %>
<%= render @outletproductform %>
<div class="row">
<aside class="col-md-4">
<section class="stats">
<%= render 'shared/stats' %>
</section>
<div>
<%= link_to "Add to Outlet", new_outlet_product_path %>
<%= link_to "Edit", edit_product_path %>
<%= link_to "Back to products", products_path %>
</div>
</aside>
</div>
_outletproductform.html.erb
<div>
<table class="styled-table">
<thead>
<tr>
<th>Outlet</th>
<th>Quantity</th>
<th>Selling Price</th>
<th>Last Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= outlet_product.outlet.name %></td>
<td><%= outlet_product.quantity %></td>
<td><%= outlet_product.selling_price %></td>
<td><%= outlet_product.last_cost %></td>
</tr>
</tbody>
</div>
架構中的 OutletProduct 遷移表
create_table "outlet_products", force: :cascade do |t|
t.integer "outlet_id"
t.integer "product_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.decimal "selling_price"
t.decimal "last_cost"
t.decimal "quantity"
結尾
控制臺中顯示錯誤
錯誤顯示在網站上
uj5u.com熱心網友回復:
如評論中所述,您將@outletproductform傳遞給render方法,但由于您尚未定義它,因此它為零,因此出現錯誤。
這適用于上一行,因為您有一個有效的@product物件,并且 render 方法正在尋找相應的視圖檔案。(例如,如果您傳遞產品串列,它將查找index.html.erb視圖等)
由于您已經在正確的位置擁有正確的視圖檔案,views/products/_outletproductform,您只需將其更改為:
<%= render 'outletproductform' %>
請注意,如果您正在尋找不在同一檔案夾中的部分(在本例中為products),您需要參考正確的路徑,即
<%= render 'some_model/some_partial' %>
這是布局和渲染指南
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510034.html
標籤:轨道上的红宝石红宝石
上一篇:編輯哈希中的單個鍵
