0

我已經檢查了有關如何創建嵌套屬性窗體的主題,但無法使其工作。嵌套屬性問題的窗體和視圖

我有兩個型號:

class LogFile < ActiveRecord::Base 
    has_one :ConfigFile 
    accepts_nested_attributes_for :ConfigFile, allow_destroy: true 
end 

class ConfigFiles < ActiveRecord::Base 
    belongs_to :LogFile 
end 

logfile的控制器我有:

def log_file_params 
     params.require(:log_file).permit(:name, 
             ..., 
             :config_file_id, 
             config_files_attributes: [:id, :json, :_destroy]) 
end 

而且形式如下:

<%= f.simple_fields_for :config_file do |n| %> 
     <%= n.input :json %> 
    <% end %> 

我需要填充爲了成爲log_file模型的config_file_id字段能夠獲取特定日誌文件的配置文件。我試圖將config_files_attributes更改爲config_file_attributes。此外,TI改變

<%= f.simple_fields_for :config_file do |n| %> 

<%= f.simple_fields_for :config_file_attributes do |n| %> 
<%= f.simple_fields_for :config_files_attributes do |n| %> 

但似乎我不能創建在config_files表中的記錄(它始終是空的)。

任何人都可以告訴我做錯了什麼嗎?


我有固定的駱駝語法和log_file_id列添加到config_file表。 我還沒有能夠填充nested表。

這是_form

<%= f.simple_fields_for :config_file_attributes do |n| %> 
      <%= n.input :json %> 
     <% end %> 

這是controller

# Never trust parameters from the scary internet, only allow the white list through. 
    def log_file_params 
     params.require(:log_file).permit(:name, 
             :description, 
             :log_file, 
             :access_type_id, 
             :config_file_id, 
             config_file_attributes: [:id, :json, :_destroy]) 
    end 

,這裏是調試輸出(注意,沒有關於CONFIG_FILE信息傳遞):

--- !ruby/object:LogFile 
attributes: 
    id: 2 
    name: Tetsd2 
    description: '123' 
    created_at: 2014-09-04 09:34:48.141041000 Z 
    updated_at: 2014-09-04 14:08:20.652419000 Z 
    log_file: test.txt 
    access_type_id: 2 
    config_file_id: 
+1

顯示您的控制器! – RubyOnRails 2014-09-04 11:57:20

+0

刪除log_file_params方法中的'config_file_id',並顯示完整的控制器。 – Thanh 2014-09-04 14:26:21

回答

2

作爲數據庫轉換belongs_to應該持有引用co lumn所以請嘗試在ConfigFiles表中添加log_id解決您的問題。在嵌套屬性中,Rails也是如此。我曾嘗試過has_many協會。

此外您還沒有指定has_one並屬於camel case。所以也正確。也在accept_nested_attributes_for中。

+0

謝謝,我已經解決了這個問題,但仍無法使其工作。你可以檢查問題編輯? – gotqn 2014-09-04 14:18:54

+0

好吧lokking到它 – 2014-09-04 14:19:25

+0

讓我知道如果我需要提供其他細節。 – gotqn 2014-09-04 14:22:13

1

你傳遞給關聯方法符號是錯誤的:

belongs_to :LogFile 

這應該是:

belongs_to :log_file 

通知駱駝外殼。

同樣,下面也有錯:

class LogFile < ActiveRecord::Base 
    has_one :ConfigFile 
    accepts_nested_attributes_for :ConfigFile, allow_destroy: true 
end 

而且應該是:

has_one :config_file 
    accepts_nested_attributes_for :config_file, allow_destroy: true 

我覺得你strong_parameters電話是錯誤的:

params.require(:log_file).permit(:name, 
    # ... 
    config_files_attributes: [] 

由於日誌文件只一個ConfigFile,你應該刪除s。它應該是config_file_attributes而不是config_files_attributes

+0

謝謝,我已經解決了這個問題,但仍然無法使其工作。你可以檢查問題編輯? – gotqn 2014-09-04 14:19:29