简体   繁体   English

在Rails中保存嵌套对象之前检查条件

[英]Check a condition before saving a nested object in Rails

I have 3 model here : NewWord , VerbForm and AdjForm . 我这里有3个模型: NewWordVerbFormAdjForm

In NewWord model , I have a column word_type stored type of word: Adj Noun Verb Phrase GenericWord NewWord模型中,我有一列word_type存储的单词类型: Adj Noun Verb Phrase GenericWord

Each NewWord may have 1 VerbForm or 1 AdjForm 每个NewWord可以具有1个VerbForm或1个AdjForm

Class NewWord < ApplicationRecord

    has_one :adj_form, dependent: :destroy
    has_one :verb_form, dependent: :destroy
    accepts_nested_attributes_for :adj_form, allow_destroy: true
    accepts_nested_attributes_for :verb_form, allow_destroy: true

   def self.types
        %w(Adj Noun Verb Phrase GenericWord)
   end
end

class NewWord::AdjForm < ApplicationRecord
    belongs_to :new_word
end

class NewWord::VerbForm < ApplicationRecord
    belongs_to :new_word
end

I use this form to create a word alongside with it forms 我用这个表格来创建一个单词

<%= simple_form_for new_word, remote: true do |f| %>
    <div class="error_section"></div>
    <%= f.input :word %>
    <%= f.input :kanji_version %>
    <%= f.input :word_type, collection: NewWord.types %>
    <%= f.simple_fields_for :verb_form do |v| %>
        <%= v.input :verb_type %>
        <%= v.input :dictionary_form %>
        # Other fields
    <% end %>
    <%= f.simple_fields_for :adj_form do |a| %>
        <%= a.input :adj_type %>
        # Other fields
    <% end %>
    <%= f.button :submit %>
<% end %>

My idea here is when user select word_type from dropdown, I can use Javasript to hide or show fields for AdjForm or VerbForm , or both. 我的想法是,当用户从下拉列表中选择word_type时,我可以使用Javasript隐藏或显示AdjFormVerbForm或两者的字段。 Then at submit, I only save AdjForm if new word's word_type is 'Adj', or VerbForm if word_type is 'Verb'. 然后在提交时,如果新单词的word_type为'Adj',则仅保存AdjForm VerbForm如果word_type为'Verb',则仅保存AdjForm

So, how can I achieve this ? 那么,我该如何实现呢? Since nested object saved automatically when I run this in new word create method: @new_word.save. 因为当我在新单词create方法中运行嵌套对象时会自动保存它: @new_word.save. ?

I have try reject_if but it only returns params for nested object only! 我有尝试reject_if但它只返回嵌套对象的参数!

accepts_nested_attributes_for :adj_form, allow_destroy: true, reject_if: :not_adj

def not_adj(att)
    att['new_word']['word_type'] != 'Adj'   # Found out "att" here only has attributes of AdjForm , not NewWord !
end

In the controller, before saving, check the word_type value and discard the params that you dont want to save. 在控制器中,在保存之前,请检查word_type值并丢弃您不想保存的参数。

new_words_controller.rb new_words_controller.rb

def create
  prepare_params     
  @new_word = NewWord.new(new_word_params)
  if @new_word.save
    # ...
  else
    # ...
  end
end

private
def prepare_params
  params.delete[:verb_form] if params[:word_type] == "Adj"
  params.delete[:adj_form] if params[:word_type] == "Verb"
  params
end

This assumes you have whitelisted the parameters for new_word and its associations. 假设您已将new_word及其关联的参数列入白名单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:在构建对象时/保存对象之前检查是否存在符合特定条件的关联? - Rails: Check while building/before saving an object if association matching certain condition is there? 如何在保存Rails 4之前检查现有记录? - How to check existing record before saving in Rails 4? Rails-在保存之前迭代has_many关联的嵌套属性 - Rails - Iterating a Nested Attributes of has_many association before saving Rails 模型:如何在保存之前计算(使用过滤器)嵌套实体 - Rails model: How to count (with filter) nested entities before saving Rails - Paperclip - 如何在保存之前检查图像尺寸 - Rails - Paperclip - How to Check the Image Dimensions before saving 在Rails中保存模型中的记录之前,执行检查重复项的功能 - Performing a function to check for duplicates before saving the record in the model in Rails Rails 3 Paperclip Uploadify:在保存对象之前保存上传的对象附件 - Rails 3 Paperclip Uploadify : Save Uploaded Object Attachments Before Saving Object 在保存到数据库之前检查记录是否存在-Rails,Twitter,ActiveRecord - check if record exists before saving to db - Rails, Twitter, ActiveRecord Rails在显示之前检查对象存在的方式 - Rails way to check for object existence before display Rails 3 - 在更新之前检查该对象是否对params有效 - Rails 3 - check that object is valid with params before update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM