I have a model of product and I need to write in the _form view , the number of the product that an admin wants to insert. I have another table with the Supply (number of product) so in my product table I don't have the attribute quantity , but I have just the
supply_id (that links my two tables of product and supply)
Since I don't have the quantity in my product table, I used a virtual attribute on Product.
I had to change the view of the new and edit product cause in the new I want the field quantity but in the edit I don't want (cause I use another view to do this) So, I deleted the partial _form and created separate view. Also, I had to set in the controller of products that if I want to update a product, i have to call a set_quantity callback, cause I have to insert a "fake" value to fill the
params[:product][:quantity]. This , because I setted the validation presence true ,on the quantity virtual field in the product model. I want to know , if all this story is right (it works , but I want a suggestion about the programming design of this story. Cause I don't like the fact that I give a fake value to fill the quantity field when I have to update a product)
class ProductsController < ApplicationController include SavePicture before_action :set_product, only: [:show, :edit, :update, :destroy] before_action :set_quantita, only: [:update] .... def set_quantita params[:product][:quantita]=2 #fake value for the update action end .... end
class Product < ActiveRecord::Base belongs_to :supply ,dependent: :destroy attr_accessor :quantita validates :quantita, presence:true end
Can you say me if there is a better way to fill the
param[:product][:quantity] in the case of the update action? Cause i don't like the fact that i give it the value of 2. Thank you.