【问题标题】:Create new model record or see if it already exists using nested_attributes form - Rails 4使用nested_attributes 表单创建新模型记录或查看它是否已经存在 - Rails 4
【发布时间】:2016-09-04 04:42:41
【问题描述】:

我正在使用 has_nested_attributes_for 创建两个不同模型(父级和子级)的两条记录。目前使用 has_nested_attributes,我在父级上的 new.html.erb 表单成功创建了父级和子级并将它们关联在一起。但是,父记录可以拥有_many 个与之关联的子模型。因此,从新表单中,我需要能够输入 url 属性(父模型上的列),如果 url 已经存在......它应该作为已经存在的 Parent 弹出(我可以使用'rails-jquery -如果需要 jquery,则为此自动完成'gem)...从而在表单上设置现有的父 ID。但是,如果它尚不存在,则该表单应创建一个新的父记录和子记录,就像当前成功发生的那样。

我需要如何更改我的控制器和视图来完成这种条件嵌套表单?谢谢,

父控制器:

class StoriesController < ApplicationController

  def new
    @story = Story.new
    video = @story.videos.build
  end

  def create
    @story = Story.new(story_params)
    if @story.save
      flash[:success] = "Your story video has been created."
      redirect_to current_narrator
    else
      flash[:alert] = "Your story or video could not be saved.  Please include all fields."
      render 'new'
    end
  end

  private

  def story_params
    params.require(:story).permit(:headline, :url, videos_attributes: [
          :imovie,
          :director_id
        ],)
  end
end

应用程序/视图/故事 new.html.erb:

<!-- New Story Nested Form -->
    <% provide(:title, 'New Story') %>
    <div class="container s-in-reg">
        <div class="authform">

        <h1>New Story</h1>

        <%= form_for @story do |f| %>

        <div class="field">
         <%= f.label :headline %><br />
         <%= f.text_field :headline %>
        </div><br/>

        <div class="field">
         <%= f.label :url %><br />
         <%= f.text_field :url %>
        </div><br/>

    <%= f.fields_for :videos do |builder| %>
        <div class="field">
          <%= render 'video_fields', f: builder %>
           # Video_fields partial contains the nested video fields required
        </div>
    <% end %>

            <%= f.submit "Post this story", class: "btn btn btn-info" %>
        <% end %>

        </div>
    </div>

Story.RB 模型:

has_many :videos

accepts_nested_attributes_for :videos

validates :headline, presence: true
validates :url, presence: true, uniqueness: true

Video.RB 模型:

class Video < ActiveRecord::Base
  belongs_to :story
  belongs_to :user

  has_attached_file :mpeg

  has_nested_attributes_for :story
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 parent-child jquery-ui-autocomplete nested-attributes


    【解决方案1】:

    所以你想要做的是有一个child 哪个accepts_nested_attributes_for 一个parent

    基本上,最简单的解决方案是传递parent_id,如果您已经有一个想要与您的孩子关联的父母,或者如果您只是要创建它,则传递parent_attributes

    可能需要您手动检查控制器中的请求参数并删除未使用的参数以避免混淆。例如,如果你传递parent_id,你想从返回的哈希中完全删除parent_attributes,但是如果parent_id = nil,那么你想删除parent_id并离开parent_attributes

    【讨论】:

    • 感谢 shlajin...所以我已将我的子模型添加到帖子中并包括 has_nested_attributes_for :parent。但是您能告诉我该代码如何查找创建操作吗?
    • 嗯...我不太确定我在那儿跟着你。您的父模型是 Story,您的子模型是 Video - 您要创建什么:Video 并在其中关联或创建 Story 或者您想要创建 Story 并关联或创建一个还是多个Videos
    • 如果一个故事已经存在,那么表单应该识别该故事(通过用户输入的唯一 URL)并将新视频与现有故事记录相关联。如果找不到用户输入的 URL(因此不存在使用该 URL 的故事)...表单应该同时创建故事和视频并将两者关联起来。所以我想基本上两者都做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多