2016-10-22 3 views
0

내 중첩 된 리소스에 대해 form_for를 올바르게 사용하는 데 문제가 있습니다.레일 : 중첩 된 form_for 오류 : 'ActionController :: UrlGenerationError'

내가 내 모델에서 설정 한 다음 한 :

team.rb

class Team < ApplicationRecord 
    has_many :superheroes 
    accepts_nested_attributes_for :superheroes 
end 

superhero.rb

class Superhero < ApplicationRecord 
    belongs_to :team 
end 

내 경로 : routes.rb

def new 
    @team = Team.find_by_id(params[:team_id]) 
    @superhero = @team.superheroes.build 
end 

나는 내 이해 어쩌면 생각

Rails.application.routes.draw do 

    root to: 'teams#index' 

    resources :teams do 
    resources :superheroes 
    end 

    get '/teams/:team_id/superheroes/:id', to: 'superheroes#show', as: 'team_superheros' 

end 

superheroes_controller.rb에서 '/app/views/superheroes/new.html.erb'

<%= form_for [@team, @superhero] do |f| %> 
    <p>Name</p> 
    <p><%= f.text_field :name %></p> 
    <p>True Identity</p> 
    <p><%= f.text_field :true_identity %></p> 
    <p><%= f.submit 'SAVE' %></p> 
<% end %> 

마지막에 중첩 된 form_for가 올바르지 않습니다.

undefined method `team_superheros_path' 

그래서 내가
routes.rb 에 다음 리디렉션 경로를 추가 :

get '/teams/:team_id/superheroes/:id', to: 'superheroes#show', as: 'team_superheros' 

이것은 "오류로 날 나뭇잎 내가 new_superhero 페이지로 이동하면 원래는 다음과 같은 오류가 발생했습니다 : 'ActionController :: UrlGenerationError'특정 오류와 "메시지 :

No route matches {:action=>"show", :controller=>"superheroes", :team_id=>#<Team id: 1, name: "Watchmen", publisher: "DC", created_at: "2016-10-22 04:04:46", updated_at: "2016-10-22 04:04:46">} missing required keys: [:id] 

난 그냥 잘못 form_for를 이용해야합니다. watchmen.superheroes.create (이름 : "The Comedian", true_identity : "Edward Blake")를 통해 콘솔에 슈퍼 히어로를 만들 수 있으며 페이지가 생성되면 @superhero는 해당 클래스의 빈 인스턴스입니다.

어떤 도움이 필요합니까?

+0

문제가 routes.rb에 있다고 생각합니다. 전체 routes.rb를 게시 할 수 있습니까? –

+0

전체 routes.db 파일을 반영하도록 원본 게시물을 편집했습니다. :) –

답변

관련 문제