2013-06-26 57 views
7

我有3個模型:報價單,客戶和物料。每個報價有一個客戶和一個項目。當我按下提交按鈕時,我想在各自的表格中創建新的報價,新的客戶和新的項目。我看過其他問題和railscasts,或者他們不適合我的情況,或者我不知道如何實施它們。Ruby on Rails:爲一個表單和一個提交的多個模型創建記錄

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

<%= 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(#) expected, got ActiveSupport::HashWithIndifferentAccess(#) 

任何幫助將不勝感激。

+0

嘗試製作:名稱和:描述可訪問和:名字和:姓氏在各自的模型中可訪問。 – lilwupster

+0

@lilwupster我做到了,但我仍然得到相同的錯誤。 – notblakeshelton

回答

4

提交一個表單,它是你需要使用accepts_nested_attributes_for

要做到這一點有關聯的兒童,則需要在模型你要使用(在你的情況下,控制器宣佈它,它看起來像報價控制器。

class Quote < ActiveRecord::Base 
    attr_accessible :quote_number 
    has_one :customer 
    has_one :item 
    accepts_nested_attributes_for :customers, :items 
end 

另外,你需要確保你宣佈哪些attributes are accessible這樣你就可以避免其他質量分配錯誤。

+0

welp,我把accepted_nested_attributes_for:customers,:items也試過了:customer,:item但是都沒有工作。 – notblakeshelton

+0

您是否檢查過以確保可訪問屬性(上面的鏈接)? – creativereason

+0

是的,我嘗試過;它沒有工作,所以我決定用attr_protected:身份證上所有的人,以確保我沒有錯過任何,但現在它吐出錯誤「的ActiveRecord :: AssociationTypeMismatch在QuotesController#創建預期
項目(#),得到的ActiveSupport :: HashWithIndifferentAccess(#)」 – notblakeshelton

1

如果你想添加的diferent車型信息,我建議申請nested_model_form李關於此參考:http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast

此解決方案非常簡單,最清潔。

+0

我是否需要更改我的模型文件?該截屏使用has_many,belongs_to關係,而我正在使用has_one和has_many,但我不知道這是否正確。我試圖將index.html.erb中的變量'@'更改爲':',但它不起作用。 – notblakeshelton

相關問題