2012-12-19 3 views
0

레일스에 처음 생겼어. 이것이 어리석은 질문이라면 용서해주십시오. - 내가 잘못 시작했는지에 대한 충분한 답을 함께 모을 수는 없습니다. . 여기에 간다. 레일 대용량 지정 제한으로 인해 스캐 폴딩 된 참조가 저장되지 않는다

  1. 은 내가 rails g scaffold Company name:string description:text location_city:string location_state:string accounttype:references

  2. Company 모델에 관련된 모든 것을 창조 내가 rails g scaffold Accounttype id:primary_key name:string price:decimal

  3. Company 모델로 Accounttype 모델에 관련된 모든 따라서 다음과 같은 수당이 포함되어 생성 :

    belongs_to :accounttype

    ,210

    attr_accessible :description, :location_city, :location_state, :name

  4. 내가 회사 내 발판으로 생성 된 편집 형태로 이동하여 해당 필드에 데이터를 입력, 그것은 오류가 발생합니다 : Can't mass-assign protected attributes: accounttype

  5. 내가 :accounttypeattr_accessible에 추가하는 경우를, 그것은 Accounttype(#70175254242100) expected, got String(#70175215042700)를 던졌습니다

    {"utf8"=>"✓", "authenticity_token"=>"oXm3cqo0jemKYFB5OGqn5ixXLSB+bH19/1RhPUu0ZHU=", "company"=>{"name"=>"Acme Corp", "description"=>"a", "location_city"=>"san diego", "location_state"=>"california", "accounttype"=>"1"}, "commit"=>"Create Company"}

다음 요청 PARAMS와 0

두 모델을 연결하기 위해 :references을 사용했는데 문제가 있습니까? 그게 내가 그걸 사용했다면 괜찮은지, 그럼 내가 만들고/작동하도록 업데이 트하려면 어떻게해야합니까?

답변

1

축하를 사용하는 자원의 중첩에 중첩 된 자원을 찾아야한다! 모델 Accounttype에 직접 속성을 할당하려면 Company 모델에 을 사용해야합니다. 그래서 같이 : 당신은 당신이 또한 accounttypes_attributes 을 추가해야 attr_accessible 사용하고 있기 때문에

belongs_to :accounttype 

accepts_nested_attributes_for :accounttype 

attr_accessible :description, :location_city, :location_state, :name, :accounttypes_attributes 

공지 사항, 내가 AccounttypeAccountType

소스 변경 제안 : 응답 @pavel에 대한 DocsRailscast

+0

철저한 답변을 위해 jason에게 감사드립니다. 그러나 이러한 변경을하더라도 여전히 동일한 오류가 발생합니다. '보호 된 속성을 대량 할당 할 수 없습니다 : accounttype' – Kristian