【发布时间】:2017-12-17 05:03:00
【问题描述】:
我目前正在尝试通过 collection_select 表单显示内容所标识的组织。
问题是在创建新联系人并选择组织后。组织不显示。
到目前为止,我已尝试在网上搜索答案,但没有任何帮助。我也尝试了所有我能想到的代码问题。
这是我的模型:
class Contact < ApplicationRecord
has_many :contact_orgs
has_many :organizations, through: :contact_orgs
accepts_nested_attributes_for :organizations
end
class Organization < ApplicationRecord
has_many :contact_orgs
has_many :contacts, through: :contact_orgs
end
class ContactOrg < ApplicationRecord
belongs_to :contact
belongs_to :organization
accepts_nested_attributes_for :organization
end
这是我的 schema.rb:
create_table "contact_orgs", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "contact_id"
t.integer "organization_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 "contacts", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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
这是我的 contact_controller.rb:
private
def contact_params
params.require(:contact).permit(:first_name, :last_name,
organizations_attributes: [:name, :industry])
end
def new
@contact = Contact.new
end
def show
@contact = Contact.find(params[:id])
end
def create
@contact = Contact.new(contact_params)
if @contact.save
redirect_to @contact
else
render 'new'
end
end
这是我的contact/new.html.erb:
<%= form_with scope: :contact, url: contact_path, local: true do |form| %>
<p>
<%= form.label :first_name %><br>
<%= form.text_field :first_name %>
</p>
<p>
<%= form.label :last_name %><br>
<%= form.text_field :last_name %>
</p>
<p>
<%= form.label :organization_id, "Organization:" %><br>
<%= form.collection_select :organization_id, Organization.order(:name), :id, :name, {}, {multiple: true} %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
这是我的contact/show.html.erb:
<p>
<strong>First name:</strong>
<%= @contact.first_name %>
</p>
<p>
<strong>Last Name:</strong>
<%= @contact.last_name %>
</p>
<hr>
<p>
<strong>Organizations:</strong>
<ul>
<% @contact.organizations.each do |organization| %>
<li><%= organization.name %></li>
<% end %>
</ul>
</p>
当我刷新我的 localhost:3000/contact/2 页面时,我的 Rails Server 显示的内容如下。
这是我的 Rails 控制台中的内容:
2.4.1 :001 > Contact.find(2).organizations
Contact Load (0.2ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Organization Load (0.2ms) SELECT "organizations".* FROM "organizations" INNER JOIN "contact_orgs" ON "organizations"."id" = "contact_orgs"."organization_id" WHERE "contact_orgs"."contact_id" = ? LIMIT ? [["contact_id", 2], ["LIMIT", 11]]=> #<ActiveRecord::Associations::CollectionProxy []>
这是我的 routes.rb 文件:
Rails.application.routes.draw do
get 'welcome/index'
resources :contacts, :organizations
root 'welcome#index'
end
提前谢谢你:)
编辑: 通过 Rails c 命令添加了 Contacts#Show 方法和 Contact.find(2).organization。
添加了 routes.rb
【问题讨论】:
标签: ruby-on-rails ruby