mardi 21 juillet 2015

Rails: Trying to save two new models with one as a FK to the other from one form

Ok, there's a chance all I need is sleep, but I've been at this for a while. Here's what I have.

I have an application to create/manage teams. These teams have a captain who can manage the team. When a user creates a new team, they should automatically be created as a captain of the team.

Links that I've referenced that I feel should be more useful than I'm making them:

I feel like the problem I'm running into is that in my situation I need to create both the team and the captain and tie them together. Where as in these links it seems like the team would already be created and the captain would be tied to it in a separate form.

Relevant code

Models:

team.rb

has_many :captain
accepts_nested_attributes_for :captain

captain.rb

belongs_to :team

Form:

<%= form_for(@team) do |f| %>

    <%= f.fields_for @team.captain_user do |tf| %>
        <%= render 'captain_users/form', locals: { form: tf } %>
    <% end %>

    <%= f.label :name %>
    <%= f.text_field :name, :required => true  %>

    <%= f.label :team_color%>
    <%= f.text_field :team_color, :required => true %>

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

This then shows the Captain fields in the same form as the team fields. But from here clicking submit does nothing. I'm not even positive if what I'm asking is possible. Right now (disregarding maybe some ajax/remote call), I'm thinking about just creating a team and then a captain and adding some notification or redirecting to captain settings.

Aucun commentaire:

Enregistrer un commentaire