I doing a grocery website. Now i want to show the outletproduct details to product page and i have create a partial file which name as outletproductform.html.erb to put at the product show page. But it show the error to me, here is my code.
Product Controller
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
show.html.erb
<% provide(:title, @product.name)%>
<%= render @product %>
<%= render @outletproductform %>
<div >
<aside >
<section >
<%= 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 >
<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 migration table in schema
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"
end
CodePudding user response:
As stated in the comments, you are passing @outletproductform to the render method, but since you havent defined it, its nil, hence the error.
this works on the previous line because you have a valid object for @product and the render method is looking for a corresponding view file. (eg if you pass a list of products it will look for the index.html.erb view and so on)
Since you already have the right view file in the right location, views/products/_outletproductform, you just have to change this to be:
<%= render 'outletproductform' %>
Note that if you are looking for a partial that isnt located in the same folder (in this case products) you would need to reference the correct path, ie
<%= render 'some_model/some_partial' %>