I followed the tutorial “Create a model through text field” (Railscast #57) which works so far. Though, I noticed that neither the select field nor the input field for the new object get validated.
I am using accepts_nested_attributes_for and validates for the affected models. Everything worked fine before I added the form fields. Now, when I submit the form without selecting or entering anything no warning occurs. This produces faulty database entries of course.
Edit 1: I added screen shots of the forms and the internship model.
- New internship form with all fields and validation
- New internship form with select and input fields from the tutorial
One example: While creating a new internship I want to be able to select a company or create a new one. I added the code as described in the tutorial but I am unsure what to do about the validation.
Here is the internship model.
class Internship < ActiveRecord::Base
belongs_to :study
belongs_to :company
attr_accessor :new_company_name, :new_company_website
before_save :create_company_from_data
accepts_nested_attributes_for :company, :study
validates :from, :presence => true
validates :till, :presence => true
validates_associated :company, :study
def create_company_from_data
create_company(:name => new_company_name, :website => new_company_website, :kind => false) unless new_company_name.blank?
end
end
I am using Rails 3.0.5.
Here is what works for me.
Here is the internship model.
Here is the form partial of the new-internship-form.
Though, I do not like that the new_company fields sit in the internship model instead in the facility model where they belong.