0

我是 Rails 的新手,我被困在一个点上,我有相关的模型 Book 和 Subject,其中 Book related_to:Subject 和 Subject has_many:books

现在我可以做些什么来存储主题字段同时保存书籍实例

书籍模型文件:book.rb

class Book < ActiveRecord::Base
  attr_accessible :title, :price,:description , :created_at ,:subject
  belongs_to :subject
  validates_presence_of :title
  validates_numericality_of :price, :message=>"Error Message"
end

主题模型文件:subject.rb

class Subject < ActiveRecord::Base
  attr_accessible :name
  has_many :books

end

我的迁移文件是书籍迁移

class Books < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.column :title, :string, :limit => 32, :null => false
      t.column :price, :float
      t.column :subject_id, :integer
      t.column :description, :text
      t.column :created_at, :string
    end
      Book.create :title =>"book1",:price =>500 , :description=>"book 1 created" , :created_at=>"12/12/12"
      Book.create :title =>"book2",:price =>111 , :description=>"book 2 created" , :created_at=>"12/12/12"
      Book.create :title =>"book3",:price =>222 , :description=>"book 3 created" , :created_at=>"12/12/12"
      Book.create :title =>"book4",:price =>333 , :description=>"book 4 created" , :created_at=>"12/12/12"
      Book.create :title =>"book5",:price =>444 , :description=>"book 5 created" , :created_at=>"12/12/12"
  end

  def self.down
    drop_table :books
  end
end

科目迁移

class Subjects < ActiveRecord::Migration
  def self.up
    create_table :subjects do |t|
      t.column :name, :string
    end
    Subject.create :name => "Physics"
    Subject.create :name => "Mathematics"
    Subject.create :name => "Chemistry"
    Subject.create :name => "Psychology"
    Subject.create :name => "Geography"
  end

  def self.down
    drop_table :subjects
  end
end

我的新书 html.erb

<html>
    <head>
        <title> new Book </title>
    </head>
    <body>
        <h1><%= @hello_message %></h1>
        <h1>Add new book</h1>
        <%= form_tag :action => 'create' %>
        <p>
            <label for="book_title">Title</label>:
            <%= text_field 'book', 'title' %>
        </p>
        <p>
            <label for="book_price">Price</label>:
            <%= text_field 'book', 'price' %>
        </p>
        <p>
            <label for="book_subject">Subject</label>:
            <%= collection_select(:book,:subject_id,@subjects,:id,:name) %>
        </p>
        <p>
            <label for="book_description">Description</label>
            <br/>
            <%= text_area 'book', 'description' %>
        </p>
        <%= submit_tag "Create" %>
        <%= form_tag %>
        <%= link_to 'Back', {:action => 'list'} %>
    </body>
</html>

当我尝试通过创建操作进行保存时,它说 Can't mass-assign protected attributes: subject_id

4

2 回答 2

1

subject在模型中添加 Accept_nested_attributes_for :books

class Subject < ActiveRecord::Base
      attr_accessible :name
      has_many :books
      accepts_nested_attributes_for :books, :allow_destroy => true

    end
于 2013-06-06T12:04:44.630 回答
1

在 Book 模型中,您应该具有:

 attr_accessible :title, :price, :description, :created_at, :subject_id
于 2013-06-06T11:58:51.663 回答