我正在使用ActiveAdmin并尝试执行一个非常简单的嵌套表单。我已经回顾了许多不起作用的类似问题的答案。
下面我创建了一个表单,其中只包含一个名为商业模型dba_number的字段。
当我尝试保存新地址时出现以下错误
无法批量分配受保护的属性:业务
我已经阅读了很多关于此安全功能的内容,甚至还查看了Ryan Bates的嵌套表单教程。我看不出有什么可以改变的。
任何人都可以向我解释我缺少什么吗?
这是我的代码
#app/models/business.rb
class Business < ActiveRecord::Base
attr_accessible :dba_number, :name, :industry_id, :address_id
has_and_belongs_to_many :owners
belongs_to :industry
belongs_to :address
end
end
#app/models/address.rb
class Address < ActiveRecord::Base
attr_accessible :street_number, :post_office_box_number, :apartment_number, :street_name, :street_suffix_id, :city, :zip_code, :state_id, :businesses_attributes, :permits_attributes
validates_presence_of :city, :zip_code
has_many :owners
has_many :businesses
has_many :permits
belongs_to :street_suffix
belongs_to :state
end
#admin/addresses.rb file
ActiveAdmin.register Address do
form :partial => 'form'
controller do
def new
new! do |format|
@address = Address.new
end
end
def create
create! do |format|
address = Menu.find(params[:address])
if @address.save
redirect_to {admin_address_url}
end
end
end
end
end
app/views/admin/addresses/_form.html.erb
<%= semantic_nested_form_for [:admin, @address] do |f| %>
<%= f.inputs "Details" do %>
<%= f.input :street_number %>
<%= f.input :post_office_box_number %>
<%= f.input :apartment_number %>
<%= f.input :street_name %>
<%= f.input :street_suffix , :as => :select, :collection => Hash[StreetSuffix.all.map{|a| [a.suffix_name, a.id]}] %>
<%= f.input :state, :as => :select, :collection => Hash[State.all.map{|s| [s.abbr, s.id]}] %>
<%= f.input :city %>
<%= f.input :zip_code %>
<%= f.inputs :dba_number, :for => :business, :name => "Business" %>
<% end %>
<%= f.actions %>
答案 0 :(得分:1)
您必须将以下代码添加到models / address.rb,否则Rails会将业务视为业务的属性
accepts_nested_attributes_for :businesses
参考:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
或者问题可能发生在
<%= f.inputs :dba_number, :for => :business, :name => "Business" %>
您将输入默认名称(如model ['attribute'])更改为'Business'。 尽量不设置名称