Railscast를 사용하여 한 번에 업데이트 할 수있는 중첩 모델이있는 페이지를 만들려고합니다. Wiki에는 많은 메모가있는 많은 주제가 있습니다.중첩 모델이 업데이트되지 않음
문제는 다음과 같습니다. a) 기존 데이터가 올바르게 수행되면 필드를 미리 채우지 않고 b) 업데이트를 수행해도 오류가 발생하지 않습니다. 실제로 성공 배너를 보여줍니다.)하지만 아무 것도하지 않습니다.
위키, 주제, note.rb (하지 실제로 읽기 위해 결합) :
class Wiki < ActiveRecord::Base
attr_accessible :topics_attributes
has_many :topics
accepts_nested_attributes_for :topics, :allow_destroy => true
end
class Topic < ActiveRecord::Base
attr_accessible :name, :notes_attributes, :wiki_id
has_many :notes
belongs_to :wiki
accepts_nested_attributes_for :notes, :allow_destroy => true
end
class Note < ActiveRecord::Base
attr_accessible :name, :info, :topic_id
belongs_to :topic
end
난
wikis_controller.rb 여기
내 코드입니다 wiki.first 일을하는 것은 오직 하나의 wiki 만 있기 때문입니다. 모델은 하나의 객체를 통해 종속 모델을 업데이트 할 수 있도록 존재합니다. 나는 그 위키 하나 이상을 가지지 않을 것이다.class WikisController < ApplicationController
before_filter :authorize
def show
@wiki = Wiki.first
end
def edit
@wiki = Wiki.first
end
def update
@wiki = Wiki.first
respond_to do |format|
if @wiki.update_attributes(params[:wiki])
format.html { redirect_to wikis_path, notice: 'Wiki was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @wiki.errors, status: :unprocessable_entity }
end
end
end
end
routes.rb 내가 하나가 "위키"원하고, 그것이있을 때까지 나는이 길을 원하는 #/위키 수 '위키'에 : 같은 '위키 # 쇼', ' 집 ' 자료 : 위키
위키/edit.html.haml
.row.container.wiki
%h1{ :style => 'margin-bottom: 40px'} Update the Wiki
= form_for @wiki do |w|
= w.fields_for :topics do |builder|
= render "topic_fields", f: builder
= link_to_add_fields "add topic", w, :topics
%br
= w.submit "Update", class: "btn"
위키/_topic_fields.html.haml
.field_inset
= f.text_field :name
= f.hidden_field :_destroy
= link_to "remove", '#', class: "remove_fields"
= f.fields_for :notes do |builder|
= render 'note_fields', f: builder
위키/_notes_fields.html.haml
.field_inset
.add_inset
= f.fields_for :notes do |builder|
.remove
= builder.text_field :name
= builder.text_field :info, class: "no_margin"
= link_to "remove", '#', class: "remove_fields"
클래스는 약간의 자바 스크립트를 호출 remove_fields의 예외와 함께, 스타일링 대부분이다. 내가 여기서 뭘 잘못하고 있는거야? 나는 이것으로 꽤 난처한 상황이다.
(PARAMS 참조하는 데 사용 [: 포스트, 실수로)를 wikis_controller 내 업데이트 작업을 수정 한 후 새로운 오류
내가 업데이트를 제출하려고 할 때, 지금이 오류가 발생합니다. 그러나 필드가 미리 입력되지 않습니다
Can't mass-assign protected attributes: notes
내가 생각했을 그 : topics_attributes과 : notes_attributes이 알아서 것 ??
ALSO 메모 필드가 미리 채워지지 않은 반면 항목 필드는 유의해야합니다.
편집 -보기의 많은 fields_for 호출에 params 객체를 파라미터가 제대로 당신은 하나가
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"3/4k8eEa4/PfeNI9cSj7zqCm9+scWBS3gwEacmc/hd0=",
"wiki"=>{"topics_attributes"=>{"0"=>{"name"=>"Pre-existing Topic Name",
"_destroy"=>"false",
"notes_attributes"=>{"0"=>{"notes"=>{"name"=>"New Notes Name",
"info"=>"New Notes Info"},
"id"=>"1"}},
"id"=>"1"}}},
"commit"=>"Update",
"id"=>"1"}
이것은 오타,하지만 수 wiki.update_attributes (PARAMS [@ 선'에서 : 포스트 ])'왜 params [: post]로 업데이트하고 있습니까? 코드에서 Post 모델을 볼 수 없으며,'params [: wiki]'여야합니다. –
굉장 해요. 다른 컨트롤러를 복사하고 그 컨트롤러를 놓쳤습니다. 위에서 언급 한 새로운 오류가 발생했습니다. – Sasha
params 해쉬를 추가 할 수 있습니까? (레일스 예외 페이지에서 대량 할당 오류가 발생하지 않아야합니다) accepts_nested_attributes_for는 괜찮아 보이지만 ** notes ** 키가 줄을 따라 어딘가에 전달되어야합니다 – Kocur4d