Ruby on Rails: создание записей для нескольких моделей с одной формой и одной отправкой
У меня есть 3 модели: цитата, клиент и товар. У каждой цитаты есть один клиент и одна вещь. Я хотел бы создать новую цитату, нового клиента и новый элемент в соответствующих таблицах, когда я нажимаю кнопку отправки. Я смотрел на другие вопросы и Railskast и либо они нене работает для моей ситуации, или я нене знаю, как их реализовать.
quote.rb
class Quote < ActiveRecord::Base
attr_accessible :quote_number
has_one :customer
has_one :item
end
customer.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
item.rb
class Item < ActiveRecord::Base
attr_accessible :name, :description
#also unsure about this
#each item can also be in multiple quotes
belongs_to :quote
quotes_controller.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
customers_controller.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
Когда я пытаюсь подтвердить это, я получаю сообщение об ошибке:
Can't mass-assign protected attributes: item, customer
Поэтому, чтобы попытаться это исправить, я обновил attr_accessible в quote.rb, добавив в него: item,: customer, но затем получаю эту ошибку:
Item(#) expected, got ActiveSupport::HashWithIndifferentAccess(#)
Любая помощь будет принята с благодарностью.