1

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 %>
2
  • It's normal that @log_file doesn't show related object, you should be able to access it inside the nested from area, but what are you trying to (access) show upon editing? Commented Sep 6, 2014 at 18:04
  • The config_file has a json field. 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. Commented Sep 6, 2014 at 18:05

1 Answer 1

1

Try using relation name without _attributes suffix:

<%= f.simple_fields_for :config_file do |n| %>
  <%= n.input :json %>
<% end %>
Sign up to request clarification or add additional context in comments.

3 Comments

It works, but it breaks the create method now. Is there any standard fix for this, or I should check if I am updating and render it without attribute part?
on create the nested form was not rendered.
ah, you need to build an object in the new action, something like @log_file.config_file.build so when the form is rendered there is a config_file for your log_file just waiting to be saved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.