I'm guessing this is more of a fundamental issue than the form simply "not rendering", but here it goes. I'll try to keep this brief but a fair amount of context may be needed.
I'm making a custom Rolodex app and the organization gave me specific things to include. For this specific problem I'm dealing with contact emails only. Ideally I would have a system like Google Contact's, where you can click to add another email field and there's a dropdown to select a category (Home, Work, etc.).
In this case the categories are coming from a table called categories. Here is a link to the entity relationship diagram I made for the entire project (not just emails): http://ift.tt/1UGLhWe
To sum things up: How do I set things up to allow the entry of emails during a contact creation/edit?
Here's my relevant code:
models/contact.rb
class Contact < ActiveRecord::Base
has_many :emails
accepts_nested_attributes_for :emails
end
models/email.rb
class Email < ActiveRecord::Base
belongs_to :contact
belongs_to :category
end
controllers/contacts_controller.rb
# GET /contacts/new
def new
@contact = Contact.new
@email = @contact.emails.build(params[:email])
end
views/contacts/_form.html.erb
<%= form_for(@contact) do |f| %>
#Other contact fields here
<% f.fields_for @email do |email| %>
<div class="field">
<%= email.label :category_id %><br>
<%= email.text_field :category_id %><br/>
</div>
<div class="field">
<%= email.label :email %><br>
<%= email.text_field :email %><br/>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I also confirmed that this whole setup works "manually". I can make contacts and categories, and then properly reference them when creating a new email by manually putting in the foreign ids. The issue here is a matter of condensing this process into one form.
Any input would be appreciated, thanks!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire