1

I am using ActiveAdmin and attempting to do a very simple nested form. I have reviewed a lot of answers to similar questions that don't work.

Below I've created a form that only includes one field called dba_number of the business model.

I get the following error when I attempt to save a new Address

Can't mass-assign protected attributes: business

I've read a lot about this security feature and even reviewed Ryan Bates's nested form tutorial. I can't see anything else to change with this.

Can anyone explain to me what I'm missing?

Here is my code

#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 %>

1 Answer 1

1

You must add following code to models/address.rb or Rails will think businesses is attribute of businesses

accepts_nested_attributes_for :businesses

reference: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

or maybe problem happen at

<%= f.inputs :dba_number, :for => :business, :name => "Business" %>

You changed the input default name(like model['attribute']) to be 'Business'. try to not to set name

Sign up to request clarification or add additional context in comments.

2 Comments

I don't understand the last sentence "You changed the input default name(like model['attribute']) to be 'Business'. try to not to set name "
Sorry for my poor english, I mean that the dba_number input will automatically be named address['dba_number'], and you override it to be 'Business' so if you use params[:address] may not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.