2012-10-03 54 views
0

我正在做一種圖書管理器,因此每個用戶都有他們的書籍集合。我所做的是這樣的rails - 從嵌套屬性獲取其他值

<%= simple_form_for @user_collection do |f|%> 
    <%= f.input :user%> 
    <%= f.input :collection_name%> 
    <%= f.simple_fields_for :collection_books do |builder|%> 
    <%= builder.input :book_name%> 
    <div class="author"><%= builder.input :author%></div> 
    <div class="year"><%= builder.input :year%></div> 
    <%end%> 
<%end%> 

我使用該視圖爲了將書籍與用戶聯繫起來。

在這裏,book_name輸入是一個自動完成字段[1],用於搜索書名,單擊書時,作者和年份輸入會填充從搜索中抓取的信息。

此外,我正在使用鏈接[2]以添加更多圖書。 ,我在這裏展示不顯示等領域, 作爲一個例子在我的收藏模型的代碼我有這個驗證線

validates_presence_of :collection_name 

所以如果我嘗試保存收集沒有COLLECTION_NAME它使得這一觀點如預期的那樣,但問題來了,因爲我只是使用訪問者屬性來顯示書籍信息,而不是因爲我想保存它們。

Collection.rb

attr_accessible :collection_books_attributes ... (and others) 
    has_many :collection_books 
    has_many :books, :through => :collection_books 
    accepts_nested_attributes_for :collection_books 

CollectionBook.rb

attr_accessible :book_name 
    attr_accessor :author, :year 

是否有存儲該值只在PARAMS沒有一個羣衆性分配錯誤的方法嗎? 因爲每次我嘗試保存時,它們都包含我創建的訪問器,以便在出現錯誤時顯示作者和書的年份(正如我之前提到的)。

希望我的問題是:)

在此先感謝

哈維爾

不夠清晰[1] http://railscasts.com/episodes/102-auto-complete-association-revised [2] http://railscasts.com/episodes/196-nested-model-form-revised

回答