我正在使用 Mongoid、awesome_nested_fields gem 和 rails 3.2.8。
我有在客户端上运行的功能(添加多个字段),但是当我尝试保存时,我收到“nil:NilClass 的未定义方法`update_attributes'”错误。
以下是所有相关信息:
配置文件.rb
class Profile
include Mongoid::Document
include Mongoid::Timestamps
has_many :skills, :autosave => true
accepts_nested_attributes_for :skills, allow_destroy: true
attr_accessible :skills_attributes
end
技能.rb
class Skill
include Mongoid::Document
belongs_to :profile
field :skill_tag, :type => String
end
看法
<%= simple_form_for(@profile) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<div class="items">
<%= f.nested_fields_for :skills do |f| %>
<fieldset class="item">
<%= f.input :skill_tag, :label => 'Skills:' %>
<a href="#" class="remove">Remove Skill</a>
<%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %>
</fieldset>
<% end %>
</div>
<a href="#" class="add">Add Skill</a>
</div>
<div class="form-actions">
<%= f.button :submit, :class => 'btn' %>
</div>
<% end %>
profile_controller.rb
# PUT /profiles/1
# PUT /profiles/1.json
def update
@profile = Profile.find(params[:id])
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' } # Notice
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
** * ** * ** * **更新** * ** * ** * ***
我最终切换到 Ryan 的 nested_form gem,它就像一个魅力。