Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Validating Form With Multiple Models
2 Posts
Validating Form With Multiple Models

I am learning Rails (and Ruby) by building your standard blog system - posts have comments and comments have an author - but am running into trouble with creating a single form that links the new comment to the post and a new author to the just created comment.

After trying one thing after the next, I have everything created and linked from a single form on the post's show action, albeit in probably not the most elegant of ways. I'll toy with that in the near future.

What I'm struggling with now is validation. I want to use the validation methods to validate that the comment has text in the body, an [a-zA-Z] author name, and a valid author email, but have not yet figured the correct combination of validats_* and error_messages_for to get the errors to display.

Here is a link to my form... http://pastie.org/363243

Eric, You can use "validates_associated":http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_associated in your Comment model on the author association. This will validate the author when the comment is saved.

2 Posts
Login to add your message