I'm new to rails and I'm creating a simple app that has clients with addersses. After getting some advice and views from the stack overflow community, I decided to save addresses as a seperate model
I am now trying to implement this in my app but I'm having problems getting the address to save correctly from the "new client" form. here is my code so far:
class Address < ActiveRecord::Base
belongs_to :client
end
class Client < ActiveRecord::Base
has_one :address
before_create :build_address, unless: Proc.new { |client| client.address }
end
<%= form_for(@client) do |f| %>
<% if @client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@client.errors.count, "error") %> prohibited this client from being saved:</h2>
<ul>
<% @client.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :phone_number %><br>
<%= f.text_field :phone_number %>
</div>
<%= f.fields_for :address do |a| %>
<div class="field">
<%= a.label :house_number %><br>
<%= a.number_field :house_number %>
</div>
<div class="field">
<%= a.label :house_name %><br>
<%= a.text_field :house_name %>
</div>
<div class="field">
<%= a.label :post_code %><br>
<%= a.text_field :post_code %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
With this, the client is created successfully but the address record is created with empty fields. No errors.
Any help would be most appreciated.
Thanks