code_junkie

Validating data of a model from another controller in Rails

Nov 12th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. def create
  2. @client = Client.new(params[:client])
  3.  
  4. respond_to do |format|
  5. if @client.save
  6. # Add purchase
  7. @sell = Purchase.new
  8. @sell.client_id = @client.id
  9. @sell.date = params[:date]
  10. # Fill another fields
  11. if @sell.save
  12.  
  13. # Do another stuff...
  14.  
  15. else
  16. format.html { render :action => "new" }
  17. format.xml { render :xml => @client.errors, :status => :unprocessable_entity }
  18. end
  19.  
  20. flash[:notice] = 'You have a new client!'
  21. format.html { redirect_to(:action => :show, :id => @evento.id) }
  22. format.xml { render :xml => @client, :status => :created, :location => @client }
  23. else
  24. format.html { render :action => "new" }
  25. format.xml { render :xml => @evento.client, :status => :unprocessable_entity }
  26. end
  27. end
  28. end
  29.  
  30. belongs_to :client
  31. validates_format_of :date, :with => /^20[0-9]{2}[-][0-9]{2}[-][0-9]{2}$/, :message => 'not valid'
  32. validates_presence_of :date
  33.  
  34. Client.purchases.empty?
  35. Client.purchases.size,
  36. Client.purchases
  37. Client.purchases<<(purchase)
  38. Client.purchases.delete(purchase)
  39. Client.purchases.find(purchases_id)
  40. Client.purchases.find_all(conditions)
  41. Client.purchases.build
  42. Client.purchases.create
Add Comment
Please, Sign In to add comment