我已經檢查了有關如何創建嵌套屬性窗體的主題,但無法使其工作。嵌套屬性問題的窗體和視圖
我有兩個型號:
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:
顯示您的控制器! – RubyOnRails 2014-09-04 11:57:20
刪除log_file_params方法中的'config_file_id',並顯示完整的控制器。 – Thanh 2014-09-04 14:26:21