0

在我的 Rails 应用程序中,我需要在表单中显示带有复选框的用户电子邮件 ID,以将用户分配到特定项目。我有一个数组对象@programmers,对象中的每一行都包含我需要在表单中显示的电子邮件 ID,并带有复选框。

我包含表单的部分视图是:

_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有:

匹配 'projects/: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 "test@gmail.com":String

我认为这是复选框哈希的问题。请帮忙。

用户.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

项目.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

作业.rb

class Assignment < ActiveRecord::Base
  attr_accessible :user_id, :project_id
  belongs_to :user
  belongs_to :project
end

请检查。我已经用 3 个模型更新了这个问题。

4

2 回答 2

1

参考这个

check_box does not work when the check box goes within an array-like parameter

以下应该对你有用

<% @programmers.each do |programmer| %>
  <%= check_box_tag "project[programmer]", programmer.email %>
<% end %>
于 2013-05-13T06:47:46.330 回答
1

假设您已正确设置关联。

<% @programmers.each do |programmer| %>
  <%= check_box_tag "project[programmer_ids][]", programmer.id, @project.programmers.include?(programmer) %>
  <%= programmer.email %>
<% end %>
于 2013-05-13T06:51:16.417 回答