我有 3 个模型:报价、客户和项目。每个报价都有一个客户和一个项目。当我按下提交按钮时,我想在他们各自的表格中创建一个新报价、一个新客户和一个新项目。我查看了其他问题和 railscast,要么它们不适用于我的情况,要么我不知道如何实施它们。
报价单.rb
class Quote < ActiveRecord::Base
attr_accessible :quote_number
has_one :customer
has_one :item
end
客户.rb
class Customer < ActiveRecord::Base
attr_accessible :firstname, :lastname
#unsure of what to put here
#a customer can have multiple quotes, so would i use has_many or belongs_to?
belongs_to :quote
end
项目.rb
class Item < ActiveRecord::Base
attr_accessible :name, :description
#also unsure about this
#each item can also be in multiple quotes
belongs_to :quote
引号_控制器.rb
class QuotesController < ApplicationController
def index
@quote = Quote.new
@customer = Customer.new
@item = item.new
end
def create
@quote = Quote.new(params[:quote])
@quote.save
@customer = Customer.new(params[:customer])
@customer.save
@item = Item.new(params[:item])
@item.save
end
end
items_controller.rb
class ItemsController < ApplicationController
def index
end
def new
@item = Item.new
end
def create
@item = Item.new(params[:item])
@item.save
end
end
客户控制器.rb
class CustomersController < ApplicationController
def index
end
def new
@customer = Customer.new
end
def create
@customer = Customer.new(params[:customer])
@customer.save
end
end
我的报价单/new.html.erb
<%= form_for @quote do |f| %>
<%= f.fields_for @customer do |builder| %>
<%= label_tag :firstname %>
<%= builder.text_field :firstname %>
<%= label_tag :lastname %>
<%= builder.text_field :lastname %>
<% end %>
<%= f.fields_for @item do |builder| %>
<%= label_tag :name %>
<%= builder.text_field :name %>
<%= label_tag :description %>
<%= builder.text_field :description %>
<% end %>
<%= label_tag :quote_number %>
<%= f.text_field :quote_number %>
<%= f.submit %>
<% end %>
当我尝试提交时出现错误:
Can't mass-assign protected attributes: item, customer
因此,为了尝试修复它,我更新了 quote.rb 中的 attr_accessible 以包含 :item, :customer 但随后出现此错误:
Item(#) expected, got ActiveSupport::HashWithIndifferentAccess(#)
任何帮助将不胜感激。