Search code examples
ruby-on-rails-3routesoncreatenested-resources

Rails 3 Create nested resource "No route matches" error


I am new to Rails and am trying to create a child record in a nested resource. My routes.rb file contains this:

resources :sports do
  resources :teams
end

My teams_controller.rb file contains this for the create def:

def create
@sport = Sport.find(params[:sport_id])
@team = @sport.teams.build(params[:team_id])

respond_to do |format|
  if @team.save
    format.html { redirect_to @team, notice: 'Team was successfully created.' }
    format.json { render json: @team, status: :created, location: @team }
  else
    format.html { render action: "new" }
    format.json { render json: @team.errors, status: :unprocessable_entity }
  end
end

end

And my _form.html.erb partial (in my new.html.erb in the app/views/teams folder code is:

<%= form_for(@team) do |f| %>
<% if @team.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@team.errors.count, "error") %> prohibited this team from being saved:</h2>
  <ul>
  <% @team.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
 <div class="field">
 <%= f.label :city %><br />
 <%= f.text_field :city %>
  </div>
  <div class="actions">
    <%= f.submit %>
</div>
<% end %>

When I try to submit on the form, I get the following error:

"No route matches [POST] "/teams" "

Lastly, when I rake routes, I get this:

sport_teams GET    /sports/:sport_id/teams(.:format)          teams#index
            POST   /sports/:sport_id/teams(.:format)          teams#create
new_sport_team GET    /sports/:sport_id/teams/new(.:format)      teams#new
edit_sport_team GET    /sports/:sport_id/teams/:id/edit(.:format) teams#edit
 sport_team GET    /sports/:sport_id/teams/:id(.:format)      teams#show
            PUT    /sports/:sport_id/teams/:id(.:format)      teams#update
            DELETE /sports/:sport_id/teams/:id(.:format)      teams#destroy
     sports GET    /sports(.:format)                          sports#index
            POST   /sports(.:format)                          sports#create
  new_sport GET    /sports/new(.:format)                      sports#new
 edit_sport GET    /sports/:id/edit(.:format)                 sports#edit
      sport GET    /sports/:id(.:format)                      sports#show
            PUT    /sports/:id(.:format)                      sports#update
            DELETE /sports/:id(.:format)                      sports#destroy

I would expect the team to save from the build on team and direct me to the show team page. Does anybody know / see what it is that I'm doing wrong?


Solution

  • You need to pass form_for an array of the sport and team:

    <%= form_for([@sport, @team]) do |f| %>
    

    Same with the redirect_to:

    format.html { redirect_to [@sport, @team], notice: 'Team was successfully created.' }
    

    From the Rails API:

    If your resource has associations defined, for example, 
    you want to add comments to the document given that the routes
    are set correctly:
    
    <%= form_for([@document, @comment]) do |f| %>
     ...
    <% end %>
    
    Where 
      @document = Document.find(params[:id]) 
    and 
      @comment = Comment.new.