简单的一对多的关系,并在服务器日志中获取一个不允许的参数。
杯1-*>接触
帕拉姆看上去像
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ay/wRwJTr4u5Vgu5oOrk7z4RC/OfeLJdN9WXuoyU7iQ=", "cup"=>{"name"=>"cupname", "location"=>"Somewhe", "type_ids"=>"1", "contact"=>{"first_name"=>"Greg", "last_name"=>"Ander", "email"=>"email@me.com", "telephone"=>"444906398"}}, "commit"=>"Create new cup"}在cups_controller.rb中
class CupsController < ApplicationController
def create
@cup = Cup.new(cup_params)
render nothing: true
end
private
def cup_params
params.require(:cup).permit(:name,:location,:type_ids,
:contacts_attributes => [:first_name, :last_name, :email, :telephone ])
end在杯/new.html.erb-f是杯的主要形式。
<%= f.fields_for :contacts do |contact_info| %>
<%= contact_info.label :first_name %><%= contact_info.text_field :first_name %>
<%= contact_info.label :last_name %><%= contact_info.text_field :last_name %>
<%= contact_info.label :email %><%= contact_info.email_field :email %>
<%= contact_info.label :telephone %><%= contact_info.telephone_field :telephone %>
<% end %>继续找Unpermitted parameter contact。
杯型有accepts_nested_attributes_for :contacts
模型文件
class Cup < ActiveRecord::Base
has_many :contacts
has_and_belongs_to_many :types
accepts_nested_attributes_for :contacts
end
class Contact < ActiveRecord::Base
belongs_to :cup
end编辑
添加了更新的cup_params和控制器类。也是模特。
发布于 2015-01-12 00:58:10
您需要引用contacts_attributes,而不是联系自己。
def cup_params
params.require(:cup).permit(:name,:location,:type_ids,
contacts_attributes: [:first_name, :last_name, :email, :telephone ])
endhttps://stackoverflow.com/questions/27893850
复制相似问题