hi guys, hope u are all out there…
so this is my controller method that creates a new product(and stores
into the db)
def create
vendor_id = session[:user_id]
@product = Product.new(params[:product])
@product.add_vendor_id(vendor_id)
respond_to do |format|
if @product.save
flash[:notice] = 'Book entry successfully created.'
format.html { redirect_to(@product) }
format.xml { render :xml => @product, :status
=> :created, :location => @product }
else
format.html { render :action => “new” }
format.xml { render :xml => @product.errors, :status
=> :unprocessable_entity }
end
end
end
the view : has fields into which the user inserts some values, minus
his id given by the db, froam table users
the model:
class Product < ActiveRecord::Base
has_many :line_items
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validate :price_must_be_grater_than_zero
validates_format_of :image_url,
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG or PNG
image.’
def self.search(search)
find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
end
def self.find_products_for_sale
find(:all, :order => "title" )
end
def self.add_vendor_id(vendor_id)
self.vendor_id = vendor_id
end
protected
def price_must_be_grater_than_zero
errors.add(:price, 'should be at least $ 0.01') if
price.nil? || price < 0.01
end
end
The problem is that rails will not save the products object giving
this error :
NoMethodError in ProductsController#create
undefined method `add_vendor_id’ for #Product:0xb60624ec
Pls help…
a billion thx in advance,
radu