Reputation: 7386
I've spent hours on this one. I'm trying to use Ryan Bate's nested_form_for plugin in rails 3.2 though I don't think it's related to my issue. I tried taking it out and I still get the same issue.
When I hit organizations/:org_id/publications/new I see the form to add a new
publication correctly. However, when I submit (with or without correct data) it
I get an unknown attribute: publications_attributes
error. I'm sure there's
something wrong with the way I'm building the related object(s) in the create
method. Any ideas?
class Organization < ActiveRecord::Base
attr_accessible :name,
:publications_attributes, # fk, nested form
has_many :publications
accepts_nested_attributes_for :publications,
:allow_destroy => true
class Publication < ActiveRecord::Base
attr_accessible :link,
:organization_id
belongs_to :organization
validates :link,
:presence => true,
end
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
resources :organizations do
resources :publications, :only => [:new, :create]
end
<%= 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 %>
<div class="field">
<%= f.label :link %>
<%= f.text_field :link %>
</div>
<%= f.link_to_remove "Remove" %>
The submitted hash looks like this:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"rhW/m8mnulZMmW7gkLYxOT8RWYoQc8eYdp2hOXkqHPU=",
"organization"=>{
"publications_attributes"=>{
"0"=>{
"link"=>"fdsfdsfs"
}
}
},
"commit"=>"Create",
"organization_id"=>"53"
}
Upvotes: 0
Views: 1303
Reputation: 706
Ok, ignore my previous answer. This is a little different than what I have done in the past, but since the organization already exists you can just call update_attributes on that. Rails (3.2 at least) will loop through all the publication_attributes and add new ones or update existing ones. In the create method try this...
if @organization.update_attributes(params[:organization])
flash[:notice] = "Successfully created publications"
redirect_to @organization
else
render :action => 'new'
end
Upvotes: 3
Reputation: 444
See it now
second line of your create
@organization.publications.build(params[:organization])
Should be
@organization.new(params[:organization])
Upvotes: 0
Reputation: 706
@organization.publications.build(params[:organization])
I think this tries to set publications_attributes on a publication, try
@organization.publications.build(params[:organization][:publication_attributes])
Upvotes: 0