当我尝试编辑联系人以添加DataType时,我收到:'未许可的参数:data_format'错误。我试过跟随我在网上发现的类似关联,但无法弄清楚我错过了什么。如果这是多余的,我道歉。
以下是相关信息:
型号:
# == Schema Information
#
# Table name: contacts
#
# id :integer not null, primary key
# name :string
# email :string
# phone :string
# mobile :string
# created_at :datetime not null
# updated_at :datetime not null
# supplier_id :integer
#
class Contact < ActiveRecord::Base
has_one :data_type
accepts_nested_attributes_for :data_type, allow_destroy: true
end
# == Schema Information
#
# Table name: data_formats
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# contact_id :integer
#
class DataFormat < ActiveRecord::Base
belongs_to :contact
end
联络管理员:
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
private
def set_contact
@contact = Contact.find(params[:id])
end
def contact_params
params.require(:contact).permit(:name, :email, :phone, :mobile, :data_format,
data_format_attributes: [ :id, :name, :contact_id, :_destroy ] )
end
end
修改视图
<%= simple_form_for @contact do |f| %>
<div class="form-group">
<div class="col-lg-6">
<%= f.input :name %>
</div>
<div class="col-lg-6">
<%= f.simple_fields_for :data_format do |t| %>
<%= t.input :id, label: 'Date Format:', :collection => DataFormat.order(:name) %>
<% end %>
</div>
<div class="col-lg-6">
<%= f.label "Submit", class: 'control-label' %>
<%= f.button :submit, class: 'btn btn-primary form-control' %>
</div>
</div>
<% end %>
DataType(s)出现在表单中,我可以选择它们。所以,我知道问题必须在控制器内。
非常感谢任何帮助!
答案 0 :(得分:1)
您的contact.rb
正在接受嵌套属性名称和permitted parameters
名称不匹配。 contact.rb
提及:accepts_nested_attributes_for :data_type, allow_destroy: true .
<强> contact.rb 强>
class Contact < ActiveRecord::Base
has_one :data_format
accepts_nested_attributes_for :data_format, allow_destroy: true
end
<强> contacts_controller.rb 强>
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
def new
@contact = Contact.new
@contact.data_format = DataFormat.new
end
...
private
def set_contact
@contact = Contact.find(params[:id])
end
def contact_params
params.require(:contact).permit(:name, :email, :phone, :mobile, :data_format,
data_format_attributes: [ :id, :name, :contact_id, :_destroy ] )
end
<强>形式强>
<%= simple_form_for @contact do |f| %>
<div class="form-group">
<div class="col-lg-6">
<%= f.input :name %>
</div>
<div class="col-lg-6">
<%= f.simple_fields_for :data_format, @contact.data_format do |t|%>
<%= t.input :id, label: 'Date Format:', :collection => DataFormat.order(:name) %>
<% end %>
</div>
<div class="col-lg-6">
<%= f.label "Submit", class: 'control-label' %>
<%= f.button :submit, class: 'btn btn-primary form-control' %>
</div>
</div>
<% end %>