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