【发布时间】:2014-09-04 11:52:19
【问题描述】:
我查看了有关如何创建嵌套属性表单的主题,但无法使其正常工作。
我有两个模型:
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。另外,我改变了
<%= 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:
【问题讨论】:
-
显示你的控制器!!!
-
删除 log_file_params 方法中的
config_file_id并请完整显示您的控制器。
标签: ruby-on-rails ruby ruby-on-rails-4 simple-form nested-attributes