表格未正确显示

时间:2013-11-08 16:43:53

标签: sql ruby-on-rails ruby ruby-on-rails-3 sqlite

我的表格包含以下型号

Employee.rb

class Employee < ActiveRecord::Base

      attr_accessible :employee_number, :joining_date, :first_name, :middle_name, :last_name,
      :gender, :job_title, :employee_department_id, :qualification, :experience_detail,
      :experience_year, :experience_month, :status_description, :date_of_birth, :marital_status,
      :children_count, :father_name, :mother_name, :husband_name, :blood_group, :nationality_id,
      :home_address_line1, :home_address_line2, :home_city, :home_state, :home_pin_code,
      :office_address_line1, :office_address_line2, :office_city, :office_state, :office_pin_code,
      :office_phone1, :office_phone2, :mobile_phone, :home_phone, :email, :fax, :user_id,
      :reporting_manager_id, :employee_grade_id, :office_country_id,
      :home_country_id, :employee_category, :employee_position_id

      belongs_to :employee_department

      has_many :counselor_supervisors
      belongs_to :employee_position

      def to_label
       full_name = first_name + " " + last_name
      end

    end

EmployeeDepartment.rb

class EmployeeDepartment < ActiveRecord::Base
      attr_accessible :code, :name

      has_many :employees

      has_many :employee_positions

      has_many :counselor_supervisors

      has_many :batch_leadership_supervisors

    def to_label
      name
    end


    end

CounselorSupervisor.rb

class CounselorSupervisor < ActiveRecord::Base
      attr_accessible :employee_id, :employee_department_id, :employee_position_id
      belongs_to :employee
      belongs_to :employee_department
      has_many :batch_counselor_supervisors

    def to_label
     employee.to_label
    end

    end

BatchCounselorSupervisor.rb

class BatchCounselorSupervisor < ActiveRecord::Base
   attr_accessible :counselor_supervisor_id , :employee_department_id , :counselor_batch_id, 
    :batch_counselor_advisors_attributes
  has_many :batch_counselor_advisors
  belongs_to :counselor_supervisor
  belongs_to :employee_department
  belongs_to :counselor_batch

  accepts_nested_attributes_for :batch_counselor_advisors

end

Employee_position.rb

class EmployeePosition < ActiveRecord::Base
  attr_accessible :position_title, :employee_department_id

  has_many :employees
  belongs_to :employee_department

  def to_label
    position_title
  end
end

batch_counselor_supervisors / new.html.erb(与我的问题相关的表格的一部分)

              <%= simple_form_for(@batch_counselor_supervisor) do |f| %>
              <%= f.error_messages %>
            <%= f.association :employee_department, as: :select %>
              <%= f.input :counselor_supervisor_id , collection: EmployeeDepartment.all, as: :grouped_select, group_method: :counselor_supervisors %>
<% end %>

下拉列表如下所示:

enter image description here

如果我添加了属于第一个部门"Business Administration"的员工,表单将正确显示如下:

enter image description here

更新:添加label_method: :to_label后,我的表单就像这样:

 <%= simple_form_for(@batch_counselor_supervisor) do |f| %>
              <%= f.error_messages %>
            <%= f.association :employee_department, as: :select %>
              <%= f.input :counselor_supervisor_id ,
               collection: EmployeeDepartment.all, as: :grouped_select, group_method: :counselor_supervisors, label_method: :to_label %>
    <% end %>

员工姓名正确显示但部门名称仍无法正确显示,如下图所示:

enter image description here

这是SQLite3问题吗?如果sqlite3问题与否,我该怎么办才能解决这个问题。

2 个答案:

答案 0 :(得分:0)

员工表和 EmployeePosition 表中的实际相关行有哪些?

员工表中,您碰巧有这两列; 的 employee_position_id employee_position 即可。

由于 employee_position 也是一个表名,因此它的bad / reduntant模型结构可能会混淆查询的include方法。在您的表中,可能会完全填写行以完成第一个查询,但即使您认为它也是如此,但不能用于任何其他查询。

答案 1 :(得分:0)

从我看到的情况来看,只有标签无法正确显示才会出现问题。您可以尝试在输入中明确设置方法:

label_method: :to_label

有关详细信息,请查看https://github.com/plataformatec/simple_form并搜索* label_method *