0

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?

models/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

models/publication.rb

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

controllers/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

views/publications/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 %>

views/publications/_form.html.erb

<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"
}
1
  • Can you add your params as they appear in your log file above, could be an alignment issue. I would grab them and try the build/create in the rails console as the lowest common denominator. `user = User.first Commented Feb 8, 2012 at 23:44

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like this is the answer! I also managed to get it to work using @organization.publications_attributes=(params[:organization][:publications_attributes]) but your solution seems way simpler. Thanks.
0
@organization.publications.build(params[:organization])

I think this tries to set publications_attributes on a publication, try

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

1 Comment

That won't work because [:publications_attributes] is a collection. But calling @organization.publications_attributes = params[:organization][:publications_attributes] should instead.
0

See it now

second line of your create

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

Should be

@organization.new(params[:organization])

1 Comment

That still gives me the same error. If it's any help adding a publications_attributes= method to the Publication model fixes the error but it then never seems to save (always invalid)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.