Rails嵌套表单的属性错误

时间:2012-02-08 18:39:23

标签: ruby-on-rails nested-forms

我在这个上花了好几个小时。我正在尝试使用Ryan Bate的nested_form_for 虽然我不认为它与我的问题有关,但在rails 3.2中插件。我试过了 把它拿出来我仍然会遇到同样的问题。

当我点击 organizations /:org_id / publications / new 时,我看到要添加新表单的表单 正确出版。但是,当我提交(有或没有正确的数据)时 我收到unknown attribute: publications_attributes错误。我确定有 我在构建中构建相关对象的方式有问题 方法。有什么想法吗?

模型/ organization.rb

class Organization < ActiveRecord::Base
  attr_accessible :name,
                  :publications_attributes, # fk, nested form
  has_many :publications
  accepts_nested_attributes_for :publications,
                                :allow_destroy => true

模型/ publication.rb

class Publication < ActiveRecord::Base
  attr_accessible :link,
                  :organization_id
  belongs_to :organization
  validates :link,
            :presence => true,
end

控制器/ publications_controller.rb

class PublicationsController < ApplicationController

  def new
    @organization = current_user.organizations.first
    @organization.publications.build
  end

  def create
    @organization = current_user.organizations.first
    @organization.publications.build(params[:organization])
    if @organization.save
      flash[:notice] = "Successfully created publications"
      redirect_to @organization
    else
      render :action => 'new'
    end
  end
end

的routes.rb

resources :organizations do
  resources :publications,  :only => [:new, :create]
end

视图/出版物/ new.html.erb

<%= nested_form_for @organization, :html => { :novalidate => 'novalidate' },
                                   :method => :post,
                                   :url => organization_publications_path, do |f| %>

  <%= render 'shared/error_messages', :object => f.object %>

  <%= f.fields_for :publications do |publication_form| %>
    <%= render :partial => 'publications/form', :locals => { :f => publication_form } %>
  <% end %>
  <%= f.link_to_add "Add", :publications %>

  <%= render :partial => 'shared/submit', :locals => { :text => 'Create', :f => f } %>
<% end %>

视图/出版物/ _form.html.erb

<div class="field">
  <%= f.label :link %>
  <%= f.text_field :link %>
</div>
<%= f.link_to_remove "Remove" %>

提交的哈希看起来像这样:

Parameters: {
    "utf8"=>"✓",
    "authenticity_token"=>"rhW/m8mnulZMmW7gkLYxOT8RWYoQc8eYdp2hOXkqHPU=",
    "organization"=>{
        "publications_attributes"=>{
            "0"=>{
                    "link"=>"fdsfdsfs"
            }
        }
    },
    "commit"=>"Create",
    "organization_id"=>"53"
}

4 个答案:

答案 0 :(得分:3)

好的,忽略我之前的回答。这与我过去的做法略有不同,但由于组织已存在,您只需调用update_attributes即可。 Rails(至少3.2)将循环遍历所有publication_attributes并添加新的或更新现有的。在create方法中试试这个...

if @organization.update_attributes(params[:organization])
  flash[:notice] = "Successfully created publications"
  redirect_to @organization
else
  render :action => 'new'
end

答案 1 :(得分:0)

我可以猜,

class Organization < ActiveRecord::Base
  attr_accessible :name,
                  :publications_attributes, # fk, nested form
  has_many :publications
  accepts_nested_attributes_for :publications,
                                :allow_destroy => true

您不需要上面代码中的 publications_attributes

答案 2 :(得分:0)

@organization.publications.build(params[:organization])

我认为这会尝试在发布上设置publications_attributes,尝试

@organization.publications.build(params[:organization][:publication_attributes])

答案 3 :(得分:0)

立即查看

您创建的第二行

@organization.publications.build(params[:organization])

应该是

@organization.new(params[:organization])