ruby-on-railsassociationshas-one

Failed to save the new associated seo_setting on has_one association


I have 2 models:

class Page < ApplicationRecord
  enum page_type: STATIC_PAGE_TYPES, _suffix: true
  has_one :seo_setting
  accepts_nested_attributes_for :seo_setting, update_only: true

  validates :title, :subtitle, length: { maximum: 50 }
  validates :page_type, uniqueness: true

  def to_param
    "#{id}-#{page_type}".parameterize
  end
end

and

class SeoSetting < ApplicationRecord
  mount_uploader :og_image, SeoSettingsOgImageUploader
  belongs_to :page

  validates :seo_title, :seo_description, :og_title, :og_description, :og_image, presence: true
end

My Page objects are created from the seeds.rb file, and when I want to edit them, I get an error: Failed to save the new associated seo_setting. In the form I have this:

<div class="card-body">
      <%= form_for([:admin, @page]) do |f| %>
        <%= render 'shared/admin/error-messages', object: @page %>
        <div class="form-group">
          <%= f.label :title, t('admin.shared.title') %>
          <%= f.text_field :title, class: 'form-control' %>
        </div>
        <div class="form-group">
          <%= f.label :subtitle, t('admin.shared.subtitle') %>
          <%= f.text_field :subtitle, class: 'form-control' %>
        </div>
        <h3>SEO Settings</h3>
        <%= f.fields_for :seo_setting, f.object.seo_setting ||= f.object.build_seo_setting do |form| %>
          <div class="form-group">
            <%= form.label :seo_title, t('admin.shared.seo_title') %>
            <%= form.text_field :seo_title, class: 'form-control' %>
          </div>
          <div class="form-group">
            <%= form.label :seo_description, t('admin.shared.seo_description') %>
            <%= form.text_area :seo_description, class: 'form-control' %>
          </div>
          <div class="form-group">
            <%= form.label :og_title, t('admin.shared.og_title') %>
            <%= form.text_field :og_title, class: 'form-control' %>
          </div>
          <div class="form-group">
            <%= form.label :og_description, t('admin.shared.og_description') %>
            <%= form.text_area :og_description, class: 'form-control' %>
          </div>
          <div class="form-group">
            <%= form.label :og_image, t('admin.shared.og_image') %>
            <div class="row">
              <div class="col-lg-12">
                <%= image_tag(form.object.og_image.url, style: 'width: 100px') if form.object.og_image? %>
              </div>
            </div>
            <%= form.file_field :og_image %>
            <%= form.hidden_field :og_image_cache %>
          </div>
        <% end %>
        <div class="form-group">
          <%= f.submit t('admin.actions.submit'), class: 'btn btn-success' %>
          <%= link_to t('admin.actions.cancel'), admin_page_path(@page) , class: 'btn btn-default' %>
        </div>
      <% end %>
    </div>

If I remove validations from my SeoSetting model, everything is working. It seems Rails doesn't like this part: f.object.build_seo_setting, because it creates a record in my database. Any ideas of how can I solve this issue? Thanks ahead.


Solution

  • Just had to change this line:

    <%= f.fields_for :seo_setting, f.object.seo_setting ||= f.object.build_seo_setting do |form| %>
    

    for this one:

    <%= f.fields_for :seo_setting, @page.seo_setting.nil? ? @page.build_seo_setting : @page.seo_setting do |form| %>