I have 2 models.
has_many :categories, :as => :categorized
validates_presence_of :name, :url, :created_at, :category
belongs_to :categorized, :polymorphic => true
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
(nothing in a select tag), i would like to display the validation
along with other validation errors for link.
My create link controller.
@link = Link.new(params[:link])
flash[:notice] = 'Link was successfully created.'
category = Category.new(params[:category][:id])
category.categorized = @link
render :action => "new"
Perhaps try enclosing the ‘save’ into a begin/rescue block?
I have something similar:
class Keyword < ActiveRecord::Base
has_many :rules, :order => :position
@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.”
flash[:notice] = "Error saving rule: " + rule_description +
flash[:notice] << $!.message
redirect_to :action => ‘show’, :id => @keyword
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 P. [email protected]