Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4332164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:13:02+00:00 2026-05-21T10:13:02+00:00

I followed the tutorial Create a model through text field (Railscast #57) which works

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:13:03+00:00Added an answer on May 21, 2026 at 10:13 am

    Here is what works for me.

    Here is the internship model.

    class Internship < ActiveRecord::Base
      attr_accessor :new_company_name, :new_company_website
    
      belongs_to :user
      belongs_to :study
      belongs_to :company, :class_name => "Facility", :foreign_key => 'facility_id'
      has_one :student, :through => :study
    
      accepts_nested_attributes_for :company, :study
    
      validates :from, :presence => true
      validates :till, :presence => true
      validates_presence_of :study
      validates_presence_of :company, :unless => :new_company_given?
      validates_presence_of :new_company_name, :new_company_website, :unless => :company_given?
    
      before_save :create_company_from_data, :if => :new_company_given?
    
    private
    
      # Returns a boolean value indicating whether a company is available.
      # @return [Boolean] True if a company is available; otherwise false.
      def company_given?
        return self.facility_id.present?
      end
    
      # Returns a boolean value indicating whether a new_company is available.
      # @return [Boolean] True if a new_company is available; otherwise false.
      def new_company_given?
        return (self.new_company_name.present? or self.new_company_website.present?)
      end
    
      # Creates a new company object taking into account the given parameters.
      def create_company_from_data
        create_company(:name => new_company_name, :website => new_company_website, :kind => false)
      end
    
    end
    

    Here is the form partial of the new-internship-form.

    <%= error_messages_for @internship %>
    
    <div class="formbox">
    
      <div class="formseparator">When?</div>
    
      <%= render :partial => "shared/internship_form_fields", :locals =>  { :internship_fields => internship_fields } %>
    
      <div class="formseparator">Where?</div>
    
      <p>
        <span class="formlabel"><%= internship_fields.label :facility_id, "Company" %></span>
        <span class="formvalue"><%= internship_fields.select :facility_id, Facility.companies.sorted.collect { |c| [c.name, c.id] }, { :include_blank => "Please select a company." }, { :class => "single" } %></span>
      </p>
    
      <p>
        <span class="formlabel"><%= internship_fields.label :new_company_name, "or create a new", :class => "optional" %></span>
        <span class="formvalue"><%= internship_fields.text_field :new_company_name, :class => "input" %></span>
      </p>
    
      <p>
        <span class="formlabel"><%= internship_fields.label :new_company_website, "with a website", :class => "optional" %></span>
        <span class="formvalue"><%= internship_fields.url_field :new_company_website, :class => "input" %></span>
      </p>
    
      <!-- End of company_fields -->
    
      <div class="formseparator">Your study?</div>
    
      <p>
        <span class="formlabel"><%= internship_fields.label :study_id, "Study" %></span>
        <% mystudies = Array.new %>
    
        <% studies = Study.sorted.collect %>
    
        <% studies.each do |study| %>
          <% if (study.student == current_user) %>
            <% mystudies.push(study) %>
          <% end %>
        <% end %>
    
        <span class="formvalue">
          <%= internship_fields.select :study_id, mystudies.collect { |s| [s.study_text, s.id] }, { :include_blank => "Please select one of your studies." }, { :class => "single" } %></span>
      </p>
    
      <!-- End of study_fields -->
    </div>
    <div class="form-buttons"><%= internship_fields.submit "Submit" %></div>
    

    Though, I do not like that the new_company fields sit in the internship model instead in the facility model where they belong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a C++ extension for python using swig. I've followed the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.