When I edit a object, the relationship object values are not displayed in the edit form. The create on other hand is working.
Here are the models:
class LogFile < ActiveRecord::Base
has_one :config_file, dependent: :destroy
accepts_nested_attributes_for :config_file, allow_destroy: true
end
class ConfigFile < ActiveRecord::Base
belongs_to :log_file
end
and this is the controller:
# GET /log_files/1/edit
def edit
end
private
# Use callbacks to share common setup or constraints between actions.
def set_log_file
@log_file = LogFile.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def log_file_params
params.require(:log_file).permit(
:name,
:user_id,
config_file_attributes: [:id, :json, :_destroy]
)
end
The form looks like this:
<%= f.simple_fields_for :config_file_attributes do |n| %>
<%= n.input :json %>
<% end %>
I have firstly try to join or include the relationship model, but was not able to do it. Some of the folks said thar id in the permit() function do the trick, but nothing changes in my situation.
Could anyone advice what to try?
Also when I put the following code in the form template:
<%= debug @log_file %>
no details about the relationship model are returned.
This is the solution I have apply using the accepted answer:
<% if @log_file.id %>
<%= f.simple_fields_for :config_file do |n| %>
<%= n.input :json %>
<% end %>
<% else %>
<%= f.simple_fields_for :config_file_attributes do |n| %>
<%= n.input :json %>
<% end %>
<% end %>
config_filehas ajsonfield. I am able to populate it on create, but when I edit a record, the field is displayed as empty. I want to display its value instead.