I'm trying to make a relationship in which a product has a great product, I'm following this tutorial link but when in the form to create a new product you can in turn create a productxpza by adding the field in the form and place in the controller:
def new
@producto = Producto.new
@producto.productosxpza.build
end
I get the following error: "undefined method 'build' for nil: NilClass" when loading the new form. I've been looking for my fault but I can not get it.
the product model:
class Producto < ActiveRecord::Base
self.primary_key = "clave"
has_one :productosxpza, class_name: "Productosxpza", foreign_key: "producto_id"
accepts_nested_attributes_for :productosxpza
end
the one of productosxpza:
class Productosxpza < ActiveRecord::Base
self.primary_key = "idp"
belongs_to :producto, class_name:"Productosxpza"
end
the product form:
<%= form_for(@producto) do |f| %>
<% if @producto.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@producto.errors.count, "error") %> prohibited this producto from being saved:</h2>
<ul>
<% @producto.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :productosxpza do |productosxpza| %>
<div class="productosxpza_fields">
<div class="field">
<%= productosxpza.label :pzaxcja %><br>
<%= productosxpza.number_field :pzaxcja %>
</div>
</div>
<%end%>
<div class="field">
<%= f.label :clave %><br>
<%= f.text_field :clave %>
</div>
<div class="field">
<%= f.label :producto %><br>
<%= f.text_field :producto %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and the product driver:
class ProductosController < ApplicationController
before_action :set_producto, only: [:show, :edit, :update, :destroy]
before_action :authenticate_usuario!
# GET /productos
# GET /productos.json
def index
@productos = Producto.all
@producto = Producto.new
end
# GET /productos/1
# GET /productos/1.json
def show
end
# GET /productos/new
def new
@producto = Producto.new
@producto.productosxpza.build
end
# GET /productos/1/edit
def edit
end
# POST /productos
# POST /productos.json
def create
@producto = Producto.new(producto_params)
respond_to do |format|
if @producto.save
format.html { redirect_to @producto, notice: 'Producto was successfully created.' }
format.json { render :show, status: :created, location: @producto }
format.js #ajax
else
format.html { render :new }
format.json { render json: @producto.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
# PATCH/PUT /productos/1
# PATCH/PUT /productos/1.json
def update
respond_to do |format|
if @producto.update(producto_params)
format.html { redirect_to @producto, notice: 'Producto was successfully updated.' }
format.json { render :show, status: :ok, location: @producto }
format.js #ajax
else
format.html { render :edit }
format.json { render json: @producto.errors, status: :unprocessable_entity }
format.js #ajax
end
end
end
# DELETE /productos/1
# DELETE /productos/1.json
def destroy
@producto.destroy
respond_to do |format|
format.html { redirect_to productos_url, notice: 'Producto was successfully destroyed.' }
format.json { head :no_content }
format.js #ajax
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_producto
@producto = Producto.find(params[:id])
end
def producto_params
params.require(:producto).permit(:clave, :producto, productosxpza_attributes: [:id, :idp, :pzaxcja, :producto_id ])
end
end