我已经检查了有关如何创建嵌套属性窗体的主题,但无法使其工作。嵌套属性问题的窗体和视图
我有两个型号:
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