0

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

Team Form:

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

    <%= f.fields_for @team.captain do |tf| %>
        <%= render 'captain/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 %>

Captain Form:

<%= f.hidden_field :user_id, value: current_user.id %>
<p>
    <%= f.label :auto_search %>
    <%= f.check_box :auto_search %>
</p>
<p>
    <%= f.label :sub_list_first %>
    <%= f.check_box :sub_list_first %>
</p>
<p>
    <%= f.label :degrees_of_separation %>
    <%= f.number_field :degrees_of_separation %>
</p>
<p>
    <%= f.label :search_timer %>
    <%= f.number_field :search_timer, value: 0 %>
</p>

teams_controller.rb

def create
    @team = Team.new(team_params)

    respond_to do |format|
        if @team.save
            # These lines added during edit
            @team.captain_users.build
            @team.captain_users.first.save

            format.html { redirect_to @team, notice: 'Team was successfully created.' }
            format.json { render json: @team.id }
        else
            format.html { render :new }
            format.json { render @team.errors, status: :unprocessable_entity }
        end
    end
end

def team_params
    # I think I need captain_attributes here also?
    params.require(:team).permit(:name, :team_color)
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.

EDIT: I seem to have it working now. I followed this guide. I'm still not positive what was happening, but I'm glad it's cleared up now.

Community
  • 1
  • 1
ELepolt
  • 333
  • 1
  • 4
  • 14
  • "But from here clicking submit does nothing." And what happens when you reload the web page (browser refresh) and then fill out the form and click submit? – MarsAtomic Jul 22 '15 at 04:02
  • Still nothing. Still very early in the project so I've dropped the db and remigrated. Tried incognito, logging in with a different user. Clicking that submit button doesn't do anything. – ELepolt Jul 22 '15 at 04:08

1 Answers1

1

It would be quite reasonable to call the relation between team and captain as

team ---> has_one :captain instead of has_many

#team.rb

has_one :captain
accepts_nested_attributes_for :captain

Then in your new method add this line @team.build_captain and in your form, change this line <%= f.fields_for @team.captain_user do |tf| %> to <%= f.fields_for :captain do |tf| %>

Update:

Include captain_attributes in team_params

def team_params
  params.require(:team).permit(:name, :team_color, captain_attributes: [:id, :auto_search, :sub_list_first, degrees_of_separation, :search_timer])
end
Pavan
  • 32,201
  • 7
  • 42
  • 69
  • I've made those changes, and it's loading the nested form fine, but still not reacting when the submit button is pressed – ELepolt Jul 22 '15 at 14:31
  • @ELepolt Can you include form code for captain fields also please? – Pavan Jul 22 '15 at 18:32
  • So close. It's accepting everything on the submit, but it seems that I can't create a captain still. What do I need on the create method to extract the captain parameters to create that new model? Thank you so much for all your help. – ELepolt Jul 22 '15 at 18:58
  • @ELepolt Can you include the server log with params generated? – Pavan Jul 22 '15 at 19:00
  • Also, I did leave the has_many captains on team.rb because I want a team to be able to have multiple captains later. Parameters: {"utf8"=>"✓", "authenticity_token"=>"EzPPRuQYLASRqVuuWieeIrtOfEE4VcwRbn8C+jr32FA4GwpM1foPMP5Yzwx2Uu9DY44nNygvCzQUO6ikL/qV6g==", "team"=>{"captain_attributes"=>{"0"=>{"user_id"=>"1", "auto_search"=>"0", "sub_list_first"=>"0", "degrees_of_separation"=>"", "search_timer"=>"0"}}, "competition_level"=>"", "coed"=>"0", "minimum_age"=>"", "name"=>"", "team_color"=>""}, "commit"=>"Create Team"} – ELepolt Jul 22 '15 at 19:07
  • So I see now that I can use @team.captain.first and get all the info inside of those parameters. Is there a best way to save that to a new captain? – ELepolt Jul 22 '15 at 19:17
  • <3 Totally forgot about that concept. – ELepolt Jul 23 '15 at 06:20