2012-04-21 20 views
1

私はRailsを初めて使用しており、ネストされたリソースに子レコードを作成しようとしています。私のroutes.rbをファイルには、これを含んでいますRails 3ネストされたリソース "No route matches"エラーを作成する

resources :sports do 
    resources :teams 
end 

マイteams_controller.rbファイルが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 

エンド

そして、私の_form.html.erb部分new.html.erbアプリ/ビュー/チームフォルダコードである:

<%= 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 %> 

私は、フォームに提出しようとすると、私は次のエラーを取得する:

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

最後に、私はルートを熊手とき、私はこれを取得:

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 

私はチームを期待しますチームのビルドから退会し、私をショーチームのページに誘導します。誰かが私が間違っていることを知っている/見ているのですか? Rails APIから

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

redirect_to

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

同じ:

答えて

2

あなたがスポーツやチームの配列form_forを渡す必要がある

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. 
+0

ことトリックをやった - ありがとう。私はそのフォームで試してみましたが、コントローラのものを無視しました(redirect_to)、それが私を汚していました。 –

関連する問題