Ruby on Rails:用一个表单和一个提交为多个模型创建记录 [英] Ruby on Rails: create records for multiple models with one form and one submit
问题描述
我有 3 个模型:报价、客户和商品.每个报价都有一个客户和一个项目.当我按下提交按钮时,我想在各自的表中创建一个新报价、一个新客户和一个新项目.我查看了其他问题和 railscasts,要么它们不适合我的情况,要么我不知道如何实施它们.
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
客户.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(#)
任何帮助将不胜感激.
推荐答案
要提交表单及其关联的子项,您需要使用 accepts_nested_attributes_for
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屋!