niftygrifty
niftygrifty

Reputation: 3652

undefined method `collection_check_boxes'

I'm trying to make an invoicing app. The form to create an invoice should include a collection of check boxes so the user can choose which lessons to invoice, but I'm getting this error: undefined method 'collection_check_boxes'.

Here are the models involved:

class Lesson < ActiveRecord::Base
  attr_accessible :lesson_time, :lesson_date, :invoice_id
  belongs_to :invoice
end

class Invoice < ActiveRecord::Base
  attr_accessible :amount, :due_date
  has_many :lessons
end

And the view:

<%= form_for(@invoice) do |f| %>
    <fieldset>     
        <%= f.label :lessons %>   
        <%= f.collection_check_boxes(:lessons, Lesson.all, :id, :lesson_date) %>         
    <%= f.submit %>
    </fieldset>
<% end %>

Upvotes: 3

Views: 1968

Answers (2)

Dan Garland
Dan Garland

Reputation: 3419

I suspect that since you tagged your post ruby-on-rails-3, you might be trying to use a rails 4 method inside a rails 3 project.

http://makandracards.com/makandra/32147-rails-4-introduced-collection_check_boxes

You'll likely need to use good old check_box_tag instead.

Upvotes: 0

Ermin Dedovic
Ermin Dedovic

Reputation: 907

collection_check_boxes is not a method of form_builder. Either put:

<%= collection_check_boxes(:lessons, Lesson.all, :id, :lesson_date) %>

This will generate html which won't associate with your model (you won't be able to use MyModel.new(params[my_model]) and expect to get proper response. You would either have to manually call my_model.lessons = params[:lessons] or you can pass a html name parameter to conform your check box name to rails convention).

Or, if you are using formtastic as you tagged it, you can use this:

<%= f.input :lessons, :as => :check_boxes, :collection => Lesson.all %>

Upvotes: 4

Related Questions