Ruby on Rails:使用一种表单和一种提交方式为多个模型创建记录 [英] Ruby on Rails: create records for multiple models with one form and one submit
问题描述
我有3个模型:报价,客户和项目.每个报价都有一个客户和一个项目.当我按下提交按钮时,我想在各自的表中创建一个新的报价,一个新的客户和一个新的项目.我查看了其他问题和相关内容,或者它们不适用于我的情况,或者我不知道如何实现它们.
I have a 3 models: quote, customer, and item. Each quote has one customer and one item. I would like to create a new quote, a new customer, and a new item in their respective tables when I press the submit button. I have looked at other questions and railscasts and either they don't work for my situation or I don't know how to implement them.
quote.rb
class Quote < ActiveRecord::Base
attr_accessible :quote_number
has_one :customer
has_one :item
end
customer.rb
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
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
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
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
my form for quotes/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 %>
当我尝试提交时出现错误:
When I try submitting that I get an error:
Can't mass-assign protected attributes: item, customer
因此,要尝试修复它,我在quote.rb中更新了attr_accessible,使其包括:item,:customer,但随后出现此错误:
So to try and fix it I updated the attr_accessible in quote.rb to include :item, :customer but then I get this error:
Item(#) expected, got ActiveSupport::HashWithIndifferentAccess(#)
任何帮助将不胜感激.
推荐答案
To submit a form and it's associated children you need to use accepts_nested_attributes_for
为此,您需要在要使用的控制器的模型上对其进行声明(在您的情况下,它看起来像是Quote Controller.
To do this, you need to declare it at the model for the controller you are going to use (in your case, it looks like the Quote Controller.
class Quote < ActiveRecord::Base
attr_accessible :quote_number
has_one :customer
has_one :item
accepts_nested_attributes_for :customers, :items
end
此外,您需要确保声明可以访问的属性 您可以避免其他批量分配错误.
Also, you need to make sure you declare which attributes are accessible so you avoid other mass assignment errors.
这篇关于Ruby on Rails:使用一种表单和一种提交方式为多个模型创建记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!