我有两个控制器,一个是project_controller.rb
,另一个是service_controller.rb
。所以我想将services_ids
存储到project
。
代码project_controller.rb
def get_service_list
@project = Project.find(params[:project_id])
end
project.rb
型号代码
class Project < ActiveRecord::Base
attr_accessible :name, :services_ids, :user_id, :vendor_id
has_many :services
end
代码service.rb
模型
class Service < ActiveRecord::Base
attr_accessible :name, :vendor_id
belongs_to :project
end
代码get_service_list.html.erb
<%= form_for(@project) do |f| %>
<%= f.collection_check_boxes :services_ids, Service.all, :id, :name %>
<%= f.submit %>
<% end %>
但它抛出此错误undefined method collection_check_boxes for
#<ActionView::Helpers::FormBuilder:0x9e996d0>
我关注了Stackoverflow和Rails API个链接
完整错误堆栈
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'
请帮助..提前致谢
答案 0 :(得分:3)
这是因为您正在使用 Rails 3
。
collection_check_boxes_tag
是introduced in Rails 4。对于您目前的使用,您必须遍历您的集合Service.all
,并为每个对象设置一个复选框:
<%= 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 %>
您还可以参考Ryan Bates for Checkboxes精彩的RailsCast。