Ruby on rails 铁路协会表格

Ruby on rails 铁路协会表格,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我只是想知道我该如何建立一个图书经理。他的模式也在效仿 |-------------|1---------*|-------------| | Customer | | BookManager | |-------------| |-------------| | ID | | ID | | First | | customer_id | | Last

我只是想知道我该如何建立一个图书经理。他的模式也在效仿

|-------------|1---------*|-------------|
| Customer    |           | BookManager |
|-------------|           |-------------|
| ID          |           | ID          |
| First       |           | customer_id |
| Last        |           | isDeleted   |
| Email       |           | isVisible   |
| Password    |           | description |
|-------------|           |-------------|
class Customer < ActiveRecord::Base
# RELATIONSHIP
    has_many :book_managers
    accepts_nested_attributes_for :book_managers, allow_destroy: :true
    attr_accessible :admin, :first_name, :last_name, :middle_name, :email, :email_confirmation, :password, :password_confirmation, :image, :book_managers_attributes,  :locale
end

class BookManager < ActiveRecord::Base
    belongs_to :customer
    attr_accessible :customer_id, :visible, :description
end
协会如下

class Customer < ActiveRecord::Base
# RELATIONSHIP
    has_many :book_managers
    accepts_nested_attributes_for :book_managers, allow_destroy: :true
    attr_accessible :admin, :first_name, :last_name, :middle_name, :email, :email_confirmation, :password, :password_confirmation, :image, :book_managers_attributes,  :locale
end

class BookManager < ActiveRecord::Base
    belongs_to :customer
    attr_accessible :customer_id, :visible, :description
end

使用名为fields_的东西来构建子模型

<%= form_for @customer do |customer_form| %>
  First name: <%= customer_form.text_field :first_name %>
  Last name : <%= customer_form.text_field :last_name %>
  <%= fields_for @book_manager do |book_manager_fields| %>
    Admin?  : <%= book_manager_fields.check_box :admin %>
  <% end %>

  <%= f.submit %>
<% end %>

名字:
姓氏:
管理员
这种性质的东西。因此,当您提交时,它会将所有数据一起发布到您的参数上

@customer.save
也可以保存图书管理员

        @customer = Customer.find(params[:id])

#       @book_manager = BookManager.new
#       bm = @customer.book_managers.build
#       bm.books.build

        @book_manager = @customer.book_managers.build
<%= form_for @customer do |customer_form| %>
  First name: <%= customer_form.text_field :first_name %>
  Last name : <%= customer_form.text_field :last_name %>
  <%= fields_for @book_manager do |book_manager_fields| %>
    Admin?  : <%= book_manager_fields.check_box :admin %>
  <% end %>

  <%= f.submit %>
<% end %>