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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 5, 11:41 am, radu puspana <radupusp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def self.add_vendor_id(vendor_id) > self.vendor_id = vendor_id > enddef ***self.***add_vendor_id means add_vendor_id makes add_vendor_id a class method, not an instance method as you want it to be. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
PROBLEM SOLVED! On Jan 5, 6:41 pm, radu puspana <radupusp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.