Hello I have 2 models. link.rb has_many :categories, :as => :categorized validates_presence_of :name, :url, :created_at, :category category.rb belongs_to :categorized, :polymorphic => true validates_presence_of :name Everything seems to work. I select my category from a select tag. The problem is i don''t know how to validate if category is for example empty (nothing in a select tag), i would like to display the validation error along with other validation errors for link. My create link controller. def create @link = Link.new(params[:link]) if @link.save flash[:notice] = ''Link was successfully created.'' category = Category.new(params[:category][:id]) category.categorized = @link category.save else render :action => "new" end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello I have 2 models. link.rb has_many :categories, :as => :categorized validates_presence_of :name, :url, :created_at, :category category.rb belongs_to :categorized, :polymorphic => true validates_presence_of :name Everything seems to work. I select my category from a select tag. The problem is i don''t know how to validate if category is for example empty (nothing in a select tag), i would like to display the validation error along with other validation errors for link. My create method from links_controller.rb def create @link = Link.new(params[:link]) if @link.save flash[:notice] = ''Link was successfully created.'' category = Category.new(params[:category][:id]) category.categorized = @link category.save else render :action => "new" end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello I have 2 models. link.rb has_many :categories, :as => :categorized validates_presence_of :name, :url, :created_at, :category category.rb belongs_to :categorized, :polymorphic => true validates_presence_of :name Everything seems to work. I select my category from a select tag. The problem is i don''t know how to validate if category is for example empty (nothing in a select tag), i would like to display the validation error along with other validation errors for link. My create link controller. def create @link = Link.new(params[:link]) if @link.save flash[:notice] = ''Link was successfully created.'' category = Category.new(params[:category][:id]) category.categorized = @link category.save else render :action => "new" end end --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Perhaps try enclosing the ''save'' into a begin/rescue block? I have something similar: class Keyword < ActiveRecord::Base has_many :rules, :order => :position <--> keywords_controller: def addrule @keyword = Keyword.find(params[:id]) rule_description = params[:rule][:description].strip # exclamation mark after create method call forces validation begin @keyword.rules.create!(:description => rule_description) flash[:notice] = "New rule was successfully added." rescue Exception flash[:notice] = "Error saving rule: " + rule_description + "<br/>" flash[:notice] << $!.message end redirect_to :action => ''show'', :id => @keyword end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
First of all I am sorry for triple post (accident). Thank you for reply. I will look into your solution. On 19 Cze, 11:20, Isabelle Phan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Perhaps try enclosing the ''save'' into a begin/rescue block? > > I have something similar: > > class Keyword < ActiveRecord::Base > has_many :rules, :order => :position > <--> > > keywords_controller: > > def addrule > @keyword = Keyword.find(params[:id]) > rule_description = params[:rule][:description].strip > # exclamation mark after create method call forces validation > begin @keyword.rules.create!(:description => rule_description) > flash[:notice] = "New rule was successfully added." > rescue Exception > flash[:notice] = "Error saving rule: " + rule_description + > "<br/>" > flash[:notice] << $!.message > end > redirect_to :action => ''show'', :id => @keyword > end > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---