使用Nested Model Form (revised) Railscast尝试创建一个具有嵌套模型的页面,可以立即更新。 Wiki有很多主题,有许多注释。
问题是,a)预先存在的数据不是按照正确执行的方式预先填充字段,并且b)点击更新不会引发任何错误(事实上,它显示成功的旗帜),但它也没有做任何事情。
这是我的代码:
wiki,topic,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 #我只想要一个“wiki”,我希望它的路由是/ wiki 获取'wikis',以:'wikis #show',as:'home' 资源:wiki
维基/的 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除外,它调用了一些javascript。知道我在这里做错了吗?我很难过这个。
新错误
在wikis_controller中修复我的更新操作后(错误地引用了params [:post]),我现在在尝试提交更新时收到此错误。不过,这些字段还没有预先填充:
Can't mass-assign protected attributes: notes
我原以为:topics_attributes和:notes_attributes可以解决这个问题吗?
ALSO ,值得注意的是,虽然Notes字段未预先填充,但“主题”字段为。
编辑 - 来自下面的质量分配错误的参数哈希
看起来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"}
答案 0 :(得分:1)
您的视图中有一对多 fields_for 。基本的经验法则是为每个 accepts_nested_attributes_for 调用设置一个 fields_for 调用。
从:
更改wikis / _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"
为:
.field_inset
.add_inset
.remove
= f.text_field :name
= f.text_field :info, class: "no_margin"
= link_to "remove", '#', class: "remove_fields"
我不是一个haml家伙,所以我不能100%确定这段代码有效,但基本的想法是删除fields_for call。
您的 notes_attributes 哈希如下所示:
"notes_attributes"=>{"0"=>{"notes"=>{"name"=>"New Notes Name",
"info"=>"New Notes Info"}, "id"=>"1"}}
它应该:
"notes_attributes"=>{"notes"=>{"name"=>"New Notes Name",
"info"=>"New Notes Info"}}