gotqn
gotqn

Reputation: 43636

form and view for nested attribute issues

I have checked the topics about how to create a nested attribute form, but can not get it work.

I have two models:

class LogFile < ActiveRecord::Base
  has_one :ConfigFile
  accepts_nested_attributes_for :ConfigFile, allow_destroy: true
end

class ConfigFiles < ActiveRecord::Base
  belongs_to :LogFile
end

In the controller of the logfile I have:

 def log_file_params
      params.require(:log_file).permit(:name,
                                       ...,
                                       :config_file_id,
                                       config_files_attributes: [:id, :json, :_destroy])
 end

And the form looks like this:

  <%= f.simple_fields_for :config_file do |n| %>
      <%= n.input :json %>
  <% end %>

I need to populate the config_file_id field of the log_file model in order to be able to get the config file for particular log file.

I have try to change the config_files_attributes to config_file_attributes. Also, ti change the

<%= f.simple_fields_for :config_file do |n| %>

to

 <%= f.simple_fields_for :config_file_attributes do |n| %>
 <%= f.simple_fields_for :config_files_attributes do |n| %>

but it seems I am not able to create a record in the config_files table (it is always empty).

Could anyone tell what I am doing wrong?


I have fix the camel case syntax and add a log_file_id column to config_file table. I am yet not able to populate the nested table.

This is the _form:

<%= f.simple_fields_for :config_file_attributes do |n| %>
          <%= n.input :json %>
      <% end %>

This is the 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

and here is the debug output (note that no information about the config_file is passed):

--- !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: 

Upvotes: 0

Views: 77

Answers (2)

Nitin
Nitin

Reputation: 7366

As a database conversion belongs_to should hold reference column so try add log_id in ConfigFiles table solve your issue. Rails also take value like so in nested attributes. I have tried it for has_many association.

Also You haven't specified has_one and belongs to in camel case. So correct that as well. Also in accept_nested_attributes_for.

Upvotes: 2

Mohamad
Mohamad

Reputation: 35349

Your symbols passed to association methods are wrong:

belongs_to :LogFile

This should be:

belongs_to :log_file

Notice the camel casing.

Likewise, the following is also wrong:

class LogFile < ActiveRecord::Base
  has_one :ConfigFile
  accepts_nested_attributes_for :ConfigFile, allow_destroy: true
end

And should be:

  has_one :config_file
  accepts_nested_attributes_for :config_file, allow_destroy: true

I think your strong_parameters call is wrong:

  params.require(:log_file).permit(:name,
    # ...
    config_files_attributes: []

Since a LogFile only has one ConfigFile, you should remove the s. It should be config_file_attributes and not config_files_attributes.

Upvotes: 1

Related Questions