Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just a banal question, if you using this in your contact form shouldn't be address in singular?</p> <pre><code> &lt;% f.fields_for :address, @contact.address do |builder| %&gt; &lt;p&gt; &lt;%= builder.label :street1, "Street 1" %&gt; &lt;/br&gt; &lt;%= builder.text_field :street1 %&gt; &lt;p&gt; &lt;% end %&gt; </code></pre> <p>In your action you have also to do</p> <pre><code>@ycontact.build_address </code></pre> <p>If you look at the source code your input field should look like.</p> <blockquote> <p>&lt; input type="text" size="30" name="contact[address_attributes][street1]" id="contact_address_attributes_street1"></p> </blockquote> <p>I think you also have problems with your associations, if you store address_id in contact then</p> <pre><code>class Contact &lt; ActiveRecord::Base belongs_to :address accepts_nested_attributes_for :address end class Address &lt; ActiveRecord::Base attr_accessible :street1 has_many :contacts end </code></pre> <p>And i think this is your case because you want to assign an address to multiple contacts.</p> <p>You can also do in inverse, if you store your contact_id in your address model:</p> <pre><code>class Contact &lt; ActiveRecord::Base has_one :address accepts_nested_attributes_for :address end class Address &lt; ActiveRecord::Base attr_accessible :street1 belongs_to :contact end </code></pre> <p>In this case you cant have users with the same address.</p> <p><strong>Update</strong></p> <p>In your user show.html.eb you can use</p> <pre><code>&lt;%= @contact.address.street1 %&gt; </code></pre> <p>In your company's show.html.erb you can display the addresses like:</p> <pre><code>&lt;% @company.addresses.each do |address| %&gt; &lt;%= address.street1 %&gt; ... &lt;% end %&gt; </code></pre> <p>On your user edit page the form fields should be the same as at new.html.erb just the form tag should look like:</p> <pre><code>&lt;% form_for @contact do |form| %&gt; &lt;%=render :partial=&gt;'fields' %&gt; &lt;% end %&gt; </code></pre> <p>The fields partial contains all your fieds togheter with the:</p> <pre><code> &lt;% f.fields_for :address, @contact.address do |builder| %&gt; &lt;p&gt; &lt;%= builder.label :street1, "Street 1" %&gt; &lt;/br&gt; &lt;%= builder.text_field :street1 %&gt; &lt;p&gt; &lt;% end %&gt; </code></pre> <p>This should work.</p> <p><strong>Update 2</strong></p> <p>You can access all the addresses which belong to a company with:</p> <pre><code>@company.addresses </code></pre> <p>If you have</p> <pre><code>class Company&lt;Activerecord::Base has_many :contacts has_many :addresses, :through=&gt;:contacts end </code></pre> <p>About the drafts, or default addresses. You can have different options depending on your needs. I you want the option that a contact could select one of the existing addresses from the database. You can do a select box in your contact form like:</p> <pre><code> &lt;%= form.select :address_id, options_from_collection_for_select(@company.addresses, 'id', 'street1')%&gt; </code></pre> <p>Also you can use some fancy javascript to add or remove the address form, in that case if the user chooses one of the existing addresses. In this case you have to remove the address_attributes from the form. I suggest to create a link with javascript code that adds the address_attributes and removes (toggle style).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload