HI all-
I have two models, Story and Tag, and they are habtm. My goal is to show the errors for @story in a form on both new and edit. Everything works fine on new/create, but it fails on edit/update. First the models:
class Story < ActiveRecord::Base attr_accessor :tag_string_holder #holds a CSV of tags ... has_and_belongs_to_many :tags, :uniq=> true ... def validate_associated_records_for_tags() end #to remove the generic "is invalid" error for tags ... validates_associated :tags, :message => 'may consist of only letters, numbers, and underscores' ...
class Tag < ActiveRecord::Base has_and_belongs_to_many :stories, :uniq => true, :order => "stories.created_at DESC" ... validates_presence_of :tag validates_format_of :tag, :with => /^[A-Za-z0-9\_]+$/, :message => 'Tags may consist of only letters, numbers, and underscores'
Ok, so here's the stories controller create method:
def create #what you do when the new form is submitted @story = Story.new(params[:story]) @story.user_id = current_user.id if request.post? begin Story.transaction do @story.write_tags @story.save! flash[:notice] = 'Story posted!' redirect_to :action => 'index' end rescue ActiveRecord::RecordInvalid => e render :action => "new" end ...
Now, this works fine, I see the correct error on the stories form (from errors_for :story) if I put an invalid tag in.
Here's the update method:
def update @story = current_user.stories.find(params[:story][:id]) @story.attributes = params[:story] if request.post? begin Story.transaction do @story.write_tags @story.save! flash[:notice] = 'Story updated!' redirect_to :action => 'index' end rescue ActiveRecord::RecordInvalid => e @story.valid? render :action => "edit" end ...
Now, I get no such error. I get nothing. If I go back and change the validates_associated to:
validates_associated :tags, :on => :update, :message => 'may consist of only letters, numbers, and underscores'
Still nothing.
In the console, if I mimic this behavior, by loading an existing story, creating a new tag, setting its tag to something invalid, when I add that tag to the story:
story.tags << tag (where tag is invalid)
An exception is thrown. After the exception, story has no errors, and tag does have an error.
So the question is, how do you use validates_associated with an already saved master on update?
Thanks for any help, Dino