0

Im struggling with a nested form in Rails 4, this is the first time that I made a form of that kind. I have read lots of documentation but Im not able to make it work. :-(

I have two models: Person and Disease. One person can have many diseases and one disease belongs to person. Looks quite simple. The form for Diseases is not saved in the database.

Person Model:

class Person < ActiveRecord::Base
  belongs_to :user
  has_many :diseases, :dependent => :destroy #if you delete a person you also delete all diseases related
  has_many :appointments, :dependent => :destroy
  validates_presence_of :name, :email
  validates :name, :length => {:maximum => 50, :too_long => "name is too long"}
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, format: { :with => VALID_EMAIL_REGEX , message: "is invalid" }

  accepts_nested_attributes_for :diseases

end

Disease Model:

class Disease < ActiveRecord::Base
  belongs_to :person
  has_many :treatments
  validates_presence_of :name, :start
  validates :name, :length => {:maximum => 50, :too_long => "is too long, you can use the description field"}
  validate :start_must_be_before_end, :unless => [:chronical, :unfinished], :presence => true
  validates :end, :presence => true, :unless => [:chronical, :unfinished], :presence => true
  validates :description, :length => {:maximum => 5000, :too_long => "is too long"}

  def start_must_be_before_end
    if self[:end] < self[:start]
      errors.add(:start, "must be before end time")
      return false
    else
      return true
    end
  end

end

People Controller:

  def create
    current_user
    @person = Person.new(person_params)
    @person.user_id = @current_user.id

    respond_to do |format|
      if @person.save
        format.html { redirect_to @person, notice: 'Person was successfully created.' }
        format.json { render action: 'show', status: :created, location: @person }
      else
        format.html { render action: 'new' }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
  end

def person_params
  params.require(:person).permit(:name, :surname, :gender, :birthdate, :bloodtype, :user_id, :phone, :email, diseases_attributes: [:id, :description] )
end

Form:

<%= simple_form_for @person do |f| %>

  <%= f.input :name %>
  <%= f.input :email %>

    <%= simple_fields_for :diseases do |my_disease| %>
      <%= my_disease.input :description %>
    <% end %>

  <%= f.button :submit %>
<% end %>

Thanks a lot for your help.

2
  • I forgot to say that without validations in the models still doesnt work. Commented Jun 12, 2014 at 9:06
  • Did my answer worked for you? Commented Jun 12, 2014 at 9:51

1 Answer 1

1

The issue is with line <%= simple_fields_for :diseases do |my_disease| %>.It should be

<%= f.simple_fields_for :diseases do |my_disease| %>

This should work.

<%= simple_form_for @person do |f| %>

  <%= f.input :name %>
  <%= f.input :email %>

  <%= f.simple_fields_for :diseases do |my_disease| %> #here
      <%= my_disease.input :description %>
   <% end %>

  <%= f.button :submit %>
<% end %>

For more Info,see this API

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that was the problem... I spent all day yesterday trying to fix it... Quite embarrassing. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.