undefined method `collection_check_boxes'

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:56:58

问题


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 %>

回答1:


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 %>



回答2:


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.



来源:https://stackoverflow.com/questions/17140274/undefined-method-collection-check-boxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!