无法在has_one关联上保存新的关联seo_setting

时间:2018-12-07 16:54:40

标签: ruby-on-rails associations has-one

我有2个型号:

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

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

我的Page对象是从seeds.rb文件创建的,当我要编辑它们时,出现错误:Failed to save the new associated seo_setting. 在表单中,我有这个:

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

如果我从SeoSetting模型中删除验证,则一切正常。 Rails似乎不喜欢这一部分:f.object.build_seo_setting,因为它在我的数据库中创建了一条记录。关于如何解决此问题的任何想法?谢谢你。

2 个答案:

答案 0 :(得分:0)

看起来问题出在这里:

accepts_nested_attributes_for :seo_setting, update_only: true

,因为您仅允许在更新时更新seo_setting

然后,当您使用此代码时:

f.object.seo_setting ||= f.object.build_seo_setting

如果缺少关联的对象,您将使用新的seo_setting

要执行此操作,您将需要删除update_only: true,或者仅在seo_setting已经存在的情况下呈现关联的字段。

答案 1 :(得分:0)

只需更改此行:

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

为此:

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