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には次のものがあります:

match '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

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

割り当て.rb

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

チェックしてください。3つのモデルで質問を更新しました。

4

2 に答える 2

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 に答える
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 に答える