0

0 and trying to use the cacoon gem for my nested form. I have installed the gem and dependencies as per the instructions and restarted the server.

I have two models: Contacts and Goals. A contact has_many goals, and a contact accepts_nested_attributes_for goals. The nested_attributes work fine, the problem is in the cacoon setup.

When I load the form (either using contacts#new or #edit action):

undefined method `done' for #<Goal:0x00000103b18790>

Below is the Contacts/Goal form:

<%= simple_form_for(@contact) do |f| %>
  <% if @contact.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
      <ul>
      <% @contact.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

    <%= f.input :name %>
    <%= f.input :title %>
    <%= f.input :company %>
    <%= f.input :email %>
    <%= f.input :notes %>

  <h3>Goals:</h3>
    <%= f.simple_fields_for(:goals) do |goal| %>

    <%= render 'goal_fields', :f => goal %>
    <%= link_to_add_association 'add goal', f, :goals %>
    <%= f.submit :submit %>
  <% end %>
<% end %>

and the partial being rendered:

<%= f.input :title %>
<%= f.input :due_date %>
<%= f.input :notes %>
<%= f.input :done, :as => :boolean %>
<%= link_to_remove_association "remove goal", f %>

Here's a link to the project: https://github.com/nowgeez/radiusapp

Any ideas? Thanks in advance!

gregnowicki
  • 301
  • 4
  • 13
  • 1
    Looks self-explanatory - does your Goal model have a 'done' attribute? If not, you can't render it on the form. – sevenseacat May 09 '14 at 01:02
  • Your Goal model definitely doesn't have a 'done' attribute. Where is that field supposed to come from and what is it supposed to render? – sevenseacat May 09 '14 at 04:42

2 Answers2

2

Ok, so their example had an input for 'done', and their model had a column for it as well.

Your model doesn't have a 'done' column, and you aren't planning on adding one at this time.

So I guess you really don't need the input on the page. You can remove

<%= f.input :done, :as => :boolean %>

So the page was complaining because you were asking it to bind input from your form, into your model, (where rails takes params[:goal] and tries to build a new Goal object), but your model doesn't have a 'done' attribute.

@RichPeck 's suggestion of a virtual attribute is a good technique to be aware of, in case you needed to hold on to some values, but weren't going to save them to the DB (though I would use this technique sparingly).

Now a few critiques on your SO question:

  1. While it is good that you linked to your github repo, it doesn't actually contain the problem code (you Gemfile doesn't have cocoon, your form isn't trying to use it, etc). So if someone clicked your repo and looked around, they wouldn't find the problem or be able to reproduce it.
  2. Apparently the gem is spelled cocoon, not cacoon. If someone was trying to look up the gem (based on your original post), and see if they could help you...they probably wouldn't find it for awhile. It is good that you posted a link to the project in your follow up comment, but it would be better if it was in the original post (OP)

When something isn't working, it is hard to know how much info to give, or what is relevant. You don't want to have a huge post with tons of crazy code, so a gist or the repo is a good way to go (which you did, but it didn't have the offending code).

The upside is that people were able to look at your post and give you a solid response really quickly. (@sevenseacat 's answer was correct, and looks like it came in pretty soon after you asked the question)

J_McCaffrey
  • 1,455
  • 12
  • 15
1

The bottom line is your error is caused because your goal model doesn't have a done attribute (there's no done column in your table)

The way to fix is to add a new column called done to your goals table (by using a rake migration) , or using a virtual attribute in your Goal model, like so:

#app/models/goal.rb
Class Goal < ActiveRecord::Base
    attr_accessor :done
end

The attr_accessor won't fix it at all... will just stop your error

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Thanks Rich, but I realize I need to be more clear - I was using the cacoon example (they have a projects/task model, where you can mark a task as :done, which you can see here: https://github.com/nathanvda/cocoon). In my app, I don't need to mark a goal done - all I want to be able to do is add/delete goals in the contacts form. – gregnowicki May 09 '14 at 13:00