Sariban D'Cl
Sariban D'Cl

Reputation: 2227

collection_check_boxes is not working throwing undefined method error (Ruby on Rails)

I have two controller one is project_controller.rb and another one is service_controller.rb. So I want to store services_ids into the project.

codes of project_controller.rb

def get_service_list
  @project = Project.find(params[:project_id])
end

codes of project.rb model

class Project < ActiveRecord::Base
  attr_accessible :name, :services_ids, :user_id, :vendor_id
  has_many :services
end

codes of service.rb Model

class Service < ActiveRecord::Base
  attr_accessible :name, :vendor_id
  belongs_to :project
end

codes of get_service_list.html.erb

<%= form_for(@project) do |f| %>
  <%= f.collection_check_boxes :services_ids, Service.all, :id, :name %>
  <%= f.submit %>
<% end %>

But its throwing this error undefined method collection_check_boxes for #<ActionView::Helpers::FormBuilder:0x9e996d0>

I followed Stackoverflow and Rails API links

Full error Stack

NoMethodError in Projects#get_service_list

Showing /home/test/ROR/vms/app/views/projects/get_service_list.html.erb where line #5 raised:

undefined method `collection_check_boxes' for #<ActionView::Helpers::FormBuilder:0xacdc9b8>

Extracted source (around line #5):

2: 
3:    
4:    <!-- %= collection_check_boxes(:projects, :services_ids,Service.all, :id, :name) % -->
5:    <%= f.collection_check_boxes :projects, :services_ids, Service.all, :id, :name_with_initial %>
6:    
7:    <%= f.submit %>
8: 

Rails.root: /home/test/ROR/vms
Application Trace | Framework Trace | Full Trace

app/views/projects/get_service_list.html.erb:5:in `block in _app_views_projects_get_service_list_html_erb___693653039_90594350'
app/views/projects/get_service_list.html.erb:1:in `_app_views_projects_get_service_list_html_erb___693653039_90594350'

Please help.. Thanks in advance

Upvotes: 0

Views: 1176

Answers (1)

kiddorails
kiddorails

Reputation: 13014

This is because you are using Rails 3.

collection_check_boxes_tag was introduced in Rails 4. For your current use, you will have to iterate over your collection Service.all and make a checkbox for each object as:

<%= form_for(@project) do |f| %>
  <% for service in Service.find(:all) %>
    <%= check_box_tag "project[services_ids][]", service.id %>
    <%= service.name %>
  <% end %>
  <%= f.submit %>
<% end %>

You can also refer a brilliant RailsCast by Ryan Bates for Checkboxes.

Upvotes: 3

Related Questions