具有多对多关系的嵌套表单 - Rails

时间:2015-02-28 20:32:57

标签: ruby-on-rails many-to-many nested-forms

我正在学习Rails,而且我很难建立一个有多对多关系的嵌套表单。

我能够通过has_many获得多对多关系:通过,但是当它创建视图和控制器以使其正常工作时,我会陷入困境。

请参阅下面的模型关系:

class Timesheet < ActiveRecord::Base
    belongs_to :user
    has_many :timesheet_payments
    has_many :employees, :through => :timesheet_payments

    accepts_nested_attributes_for :timesheet_payments,
            :reject_if => :all_blank,
            :allow_destroy => true
    accepts_nested_attributes_for :employees   
end

class Employee < ActiveRecord::Base
    belongs_to :user
    has_many :timesheet_payments
    has_many :timesheets, :through => :timesheet_payments   
end

class TimesheetPayment < ActiveRecord::Base   
    belongs_to :employee
    belongs_to :timesheet
    accepts_nested_attributes_for :employee,
                                  :reject_if => :all_blank
end

请参阅下面的数据库架构:

  create_table "timesheet_payments", force: true do |t|
    t.integer  "employee_id"
    t.integer  "timesheet_id"
    t.float    "basic_hours"
    t.float    "sunday_bh_hours"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

  create_table "timesheets", force: true do |t|
    t.date     "upload_date"
    t.integer  "user_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "employees", force: true do |t|
    t.string   "pps_no"
    t.string   "fname"
    t.string   "lname"
    t.date     "dob"
    t.text     "address"
    t.string   "ph_number"
    t.float    "basic_rop"
    t.float    "sunday_bh_rop"
    t.string   "email"
    t.date     "date_joined"
    t.integer  "user_id"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

我想创建一个表单,我可以在其中创建新的Timesheet,显示所有Employees和一个字段,为每个员工添加basic_hours和sunday_bh_hours。

这类似于客户关系的概念 - &gt;订单 - &gt;产品

我希望这是有道理的!提前谢谢!

我试过这个视图表单

<%= form_for(@payment, :html => {:multipart => true}) do |f| %>
    <% if @payment.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@payment.errors.count, "error") %> prohibited this payment from being saved:</h2>
    <% end %>

    <!--creates a new timesheet -->
    <%= f.fields_for :timesheet do |builder| %> 
          <%= builder.label "New timesheet" %>
          <%= builder.text_field :upload_date %>
          <p></p>
    <% end %>

    <!-- Add worked hours for each employee -->
    <% @employee.each do |t| %>
          <%= f.label t.fname  %>
          <br />
          <%= f.label "Basic Hours" %>
          <%= f.text_field :basic_hours  %>
          <%= f.label "Sunday/BH Hours" %>
          <%= f.text_field :sunday_bh_hours  %>
          <br />
    <% end %>

    <%= f.submit 'Submit', :class => 'btn btn-primary' %>

<% end %>

装载正常,但我真的无法理解如何创建&#34;创建&#34;控制器上的方法。

我有&#34;新&#34;方法如下:

def new 
        @payment = TimesheetPayment.new
        @timesheet = Timesheet.new
        @employee = Employee.all
end

0 个答案:

没有答案