在我的Rails應用程序中,我需要在窗體中用複選框顯示用戶電子郵件ID,以將用戶分配給特定項目。我有一個數組對象@programmers
,對象中的每一行都包含電子郵件ID,我需要在窗體內用複選框顯示。在rails中check_box字段'合併'錯誤
我的局部視圖含有形式爲:
_allocate_programmer.html.erb
<h1> Allocate programmers </h1>(Please check the programmers that you want to add)<br />
<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% unless @programmers.nil? %>
<% @programmers.each do |programmer| %>
<%= f.check_box :programmer, programmer.email %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我的routes.rb具有:
匹配 '項目/:ID/allocate_programmers' =>'projects#allocate'
我的projects_controller.rb具有下面的代碼:
def allocate
@project = Project.find(params[:id])
@programmers = User.where(:role => 'programmer')
render "_allocate_programmer"
end
我收到以下錯誤視圖
NoMethodError in Projects#allocate
Showing /home/local/Rajesh/ticket_system/app/views/projects/_allocate_programmer.html.erb where line #18 raised:
undefined method 'merge' for "[email protected]":String
我認爲它的問題與複選框哈希值。請幫忙。
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :token_authenticatable,
:rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :role
# attr_accessible :title, :body
has_many :assignments
has_many :projects, :through => :assignments
has_many :tickets
ROLES = ['admin', 'network/system admin', 'manager', 'programmer']
def role?(base_role)
ROLES.index(base_role.to_s) <= ROLES.index(role)
end
end
Project.rb
class Project < ActiveRecord::Base
attr_accessible :project_name, :description, :duration_from, :duration_upto, :user_id
has_many :assignments
has_many :users, :through => :assignments
validates :project_name, :presence => true
validates :description, :presence => true
validates :duration_from, :presence => true
validates :duration_upto, :presence => true
#validates :user_id, :presence => true //this gives error
end
Assignment.rb
class Assignment < ActiveRecord::Base
attr_accessible :user_id, :project_id
belongs_to :user
belongs_to :project
end
請CHEC ķ。我已經用3個模型更新了這個問題。
您是否充分設置了'Project'和'Programmer'之間的HABTM關係? – kiddorails 2013-05-13 06:39:26
不,我只設置了HM關係。並通過另一個稱爲分配的模型連接Project和Programmer,因爲一個項目可以有多個程序員,反之亦然。 – 2013-05-13 06:42:28
你的意思是,'has_many:程序員,:through =>:assignments'? – kiddorails 2013-05-13 06:44:52