我是 collection_select 的新手。我观看了几个Youtube视频,并在RailsGuides上阅读了有关collection_select的信息,并在Stack Overflow上进行了搜索。但是我很难理解哪些数据在哪里以及collection_select有效。
这导致我写错了我自己的collection_select。什么信息在哪里,以及我自己的collection_select中应该包含哪些信息?
我目前正在构建一个轻量级CRM并尝试为交易分配:contact_id
,但当我尝试这样做时,我不断收到错误,我在下面提供了截图。错误说明:
1错误禁止此用户被保存:
- 联系必须存在
这是我的 deals_controller.rb 文件:
class DealsController < ApplicationController
def index
@deals = Deal.all
end
def new
@deal = Deal.new
end
def show
@deal = Deal.find(params[:id])
end
def create
@deal = Deal.new(deal_params)
if @deal.save
redirect_to organization_contact_deal_url(@deal)
else
render :new
end
end
private
def deal_params
params.require(:deal).permit(:deal_name, :deal_amount, :contact_id)
end
end
以下是我的 new.html.erb 交易文件:
<%= form_with scope: :deal, url: organization_contact_deals_path, local: true do |form| %>
<% if @deal.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@deal.errors.count, "error") %> prohibited this user from being saved: </h2>
<ul>
<% @deal.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :deal_name %>
<%= form.text_field :deal_name %>
</p>
<p>
<%= form.label :contact_id %>
<%= form.collection_select(:contact_id, Contact.all, :id, :full_name) %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
这是我的 schema.rb :
ActiveRecord::Schema.define(version: 20171209203509) do
create_table "contacts", force: :cascade do |t|
t.string "contact_first_name"
t.string "contact_last_name"
t.integer "organization_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organization_id"], name:
"index_contacts_on_organization_id"
end
create_table "deals", force: :cascade do |t|
t.string "deal_name"
t.string "deal_amount"
t.integer "contact_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["contact_id"], name: "index_deals_on_contact_id"
end
create_table "organizations", force: :cascade do |t|
t.string "organization_name"
t.string "organization_industry"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
这是我的 deal.rb 模型文件:
class Deal < ApplicationRecord
belongs_to :contact
end
这是我的 contact.rb 模型文件:
class Contact < ApplicationRecord
belongs_to :organization
has_many :deals, dependent: :destroy
def full_name
"#{contact_first_name} #{contact_last_name}"
end
end
以下是路线:
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format)
welcome#index
organization_contact_deals GET /organizations/:organization_id/contacts/:contact_id/deals(.:format) deals#index
POST /organizations/:organization_id/contacts/:contact_id/deals(.:format) deals#create
new_organization_contact_deal GET /organizations/:organization_id/contacts/:contact_id/deals/new(.:format) deals#new
edit_organization_contact_deal GET /organizations/:organization_id/contacts/:contact_id/deals/:id/edit(.:format) deals#edit
organization_contact_deal GET /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format) deals#show
PATCH /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format) deals#update
PUT /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format) deals#update
DELETE /organizations/:organization_id/contacts/:contact_id/deals/:id(.:format) deals#destroy
organization_contacts GET /organizations/:organization_id/contacts(.:format) contacts#index
POST /organizations/:organization_id/contacts(.:format) contacts#create
new_organization_contact GET /organizations/:organization_id/contacts/new(.:format) contacts#new
edit_organization_contact GET /organizations/:organization_id/contacts/:id/edit(.:format) contacts#edit
organization_contact GET /organizations/:organization_id/contacts/:id(.:format) contacts#show
PATCH /organizations/:organization_id/contacts/:id(.:format) contacts#update
PUT /organizations/:organization_id/contacts/:id(.:format) contacts#update
DELETE /organizations/:organization_id/contacts/:id(.:format) contacts#destroy
organizations GET /organizations(.:format) organizations#index
POST /organizations(.:format) organizations#create
new_organization GET /organizations/new(.:format) organizations#new
edit_organization GET /organizations/:id/edit(.:format) organizations#edit
organization GET /organizations/:id(.:format) organizations#show
PATCH /organizations/:id(.:format) organizations#update
PUT /organizations/:id(.:format) organizations#update
DELETE /organizations/:id(.:format) organizations#destroy
root GET /
这是我的 routes.rb 文件:
Rails.application.routes.draw do
get 'welcome/index'
resources :organizations do
resources :contacts do
resources :deals
end
end
root 'welcome#index'
end
修改:
我更新了我的contact.rb文件,new.html.erb表单和deals_controller.rb。一旦这个问题得到解决,我将留下更详细的编辑日志。
更改后的新错误:
ActionController::UrlGenerationError in DealsController#create
No route matches {:action=>"show", :contact_id=>"2",
:controller=>"deals", :organization_id=>#<Deal id: 6, deal_name:
"Office365 deal", deal_amount: nil, contact_id: 2, created_at: "2017-
12-10 03:35:48", updated_at: "2017-12-10 03:35:48">}, missing
required keys: [:id]
@deal = Deal.new(deal_params)
if @deal.save
**redirect_to organization_contact_deal_url(@deal)**
else
render :new
end
答案 0 :(得分:0)
您从允许的参数中遗漏了:contact_id
:
def deal_params
params.require(:deal).permit(:deal_name, :deal_amount, :contact_id)
end
在联系模型中添加full_name
实例方法
class Contact < ApplicationRecord
belongs_to :organization
has_many :deals, dependent: :destroy
def full_name
"#{first_name} #{last_name}"
end
end
以您的形式:
<%= form.collection_select :contact_id, Contact.all, :id, :full_name %>
在您的控制器中创建操作:
def create
@deal = Deal.new(deal_params)
if @deal.save
redirect_to @deal
else
render :new
end
end