2012-04-21 3 views
1

레일스를 처음 사용하고 중첩 된 리소스에 하위 레코드를 만들려고합니다.레일 3 중첩 된 리소스 "경로 일치 없음"오류를 만듭니다.

resources :sports do 
    resources :teams 
end 

내 teams_controller.rb 파일이 데프 작성이 포함되어 있습니다 : 내 routes.rb 파일이 포함에서

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> 
내가 양식에 제출하려고하면

, 나는 다음과 같은 오류 얻을 :

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 

내가가 팀을 기대 : 내가 경로를 긁어 때

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

마지막을, 나는이를 얻을 수 팀 구성에서 제외하고 쇼 팀 페이지로 안내합니다. 아무도 내가 뭘 잘못하고 있는지 알지 못합니까? 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)에있는 것을 무시하고 그게 나를 더럽힌다. –

관련 문제