【问题标题】:form and view for nested attribute issues嵌套属性问题的表单和视图
【发布时间】: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


【解决方案1】:

由于数据库转换belongs_to 应该包含参考列,因此请尝试在ConfigFiles 表中添加log_id 来解决您的问题。 Rails 在嵌套属性中也具有类似的值。我已经为has_many协会尝试过。

此外,您还没有指定 has_one 并且属于驼峰式。所以也纠正一下。也在accept_nested_attributes_for中。

【讨论】:

  • 谢谢,我已经解决了这个问题,但仍然无法正常工作。你能检查一下问题编辑吗?
  • 如果我需要提供其他详细信息,请告诉我。
  • 代码中还有一些问题:比如&lt;%= f.simple_fields_for :config_file do |n| %&gt; &lt;%= n.input :json %&gt; &lt;% end %&gt;这里不需要属性
  • 忘记这个 - 一切正常。谢谢,非常感谢您指出我应该在哪里创建参考列。
【解决方案2】:

你传递给关联方法的符号是错误的:

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: []

由于一个日志文件只有一个配置文件,您应该删除s。它应该是config_file_attributes 而不是config_files_attributes

【讨论】:

  • 谢谢,我已经解决了这个问题,但仍然无法正常工作。你能检查一下问题编辑吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多