一对多关联不会保存新表单

时间:2017-12-17 06:00:54

标签: ruby-on-rails ruby

我目前正在尝试通过collection_select表单显示交易所识别的组织。

我遇到的问题是在尝试创建新交易并选择要分配给它的组织之后。我通过复数错误收到错误消息。

弹出的问题表明:

1错误禁止此交易被保存:

  • 组织必须存在

到目前为止,我已尝试在网上搜索答案,但没有任何帮助。我也尝试了一些我能想到的问题是我的代码问题。

以下是模型

class Deal < ApplicationRecord
 belongs_to :organization
 accepts_nested_attributes_for :organization
end

class Organization < ApplicationRecord
 has_many :contact_orgs
 has_many :contacts, through: :contact_orgs

 has_many :deals
end

这是我的 schema.rb

ActiveRecord::Schema.define(version: 20171217051027) do

create_table "contact_orgs", force: :cascade do |t|
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.integer "organization_id"
 t.integer "contact_id"
 t.index ["contact_id"], name: "index_contact_orgs_on_contact_id"
 t.index ["organization_id"], name: 
 "index_contact_orgs_on_organization_id"
end

create_table "deals", force: :cascade do |t|
 t.string "name"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.integer "organization_id"
 t.index ["organization_id"], name: "index_deals_on_organization_id"
end

create_table "organizations", force: :cascade do |t|
 t.string "name"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.string "industry"
end

end

这是我的 deals_controller.rb

    def show
  @deal = Deal.find(params[:id])
end

def new
  @deal = Deal.new
end

def create
  @deal = Deal.new(deal_params)

  respond_to do |format|
    if @deal.save
      format.html { redirect_to @deal, notice: 'Deal was successfully created.' }
      format.json { render :show, status: :created, location: @deal }
    else
      format.html { render :new }
      format.json { render json: @deal.errors, status: :unprocessable_entity }
    end
  end
end

private
 def deal_params
  params.require(:deal).permit(:name, organization_attributes: [:id])
 end
end

以下是我的 deals / new.html.erb 文件:

<%= form_with model: @deal, local: true do |form| %>

<% if @deal.errors.any? %>
 <div id="error_explanation">
  <h2>
    <%= pluralize(@deal.errors.count, "error") %> prohibited
    this deal from being saved:
  </h2>
  <ul>
    <% @deal.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
 </div>
<% end %>


<p>
 <%= form.label :name %><br>
 <%= form.text_field :name %>
</p>


<p>
 <%= form.label :organization %><br>
 <%= form.collection_select :organization, Organization.order(:name), :id, :name, {} %>
<p>

<p>
 <%= form.submit %>
</p>
<% end %>

提前谢谢你:)

修改

Started GET "/deals/new" for 127.0.0.1 at 2017-12-16 23:09:16 -0800
Processing by DealsController#new as HTML
Rendering deals/new.html.erb within layouts/application
Organization Load (0.6ms)  SELECT "organizations".* FROM 
"organizations" ORDER BY "organizations"."name" ASC
Rendered deals/new.html.erb within layouts/application (40.8ms)
Completed 200 OK in 183ms (Views: 108.7ms | ActiveRecord: 2.6ms)


Started POST "/deals" for 127.0.0.1 at 2017-12-16 23:09:20 -0800
Processing by DealsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bmm6VsgpA6mmU4J+3J1EWSsgEyrNmhU1LAU5HhPe9cIApFyH6p6FLfEMDIksvyNVRz8vqjXexzFu/LsalFMpTQ==", "deal"=>{"name"=>"Test Deal - 1", "organization"=>"9"}, "commit"=>"Create Deal"}
Unpermitted parameter: :organization
(0.4ms)  begin transaction
(0.1ms)  rollback transaction
Rendering deals/new.html.erb within layouts/application
Organization Load (0.7ms)  SELECT "organizations".* FROM "organizations" ORDER BY "organizations"."name" ASC
Rendered deals/new.html.erb within layouts/application (10.9ms)
Completed 200 OK in 77ms (Views: 56.6ms | ActiveRecord: 1.2ms)

1 个答案:

答案 0 :(得分:1)

应在创建子项之前创建父项。