i'm trying to save in my database some informations in a model, that have another model, like so:
class Saloon < ApplicationRecord
has_many :saloon_address
accepts_nested_attributes_for :saloon_address
end
and it has addresses:
class SaloonAddress < ApplicationRecord
belongs_to :saloon
end
and this is my view:
<%= form_with scope: :saloon, local: true, :url => "/saloon" do |form| %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :specialty %><br>
<%= form.text_field :specialty %>
</p>
<p>
<%= form.label :services %><br>
<%= form.text_field :services %>
</p>
<p>
<%= form.label :telephone %><br>
<%= form.text_field :telephone %>
</p>
<p>
<%= form.label :online %><br>
<%= form.check_box :online %>
</p>
<%= form.fields_for :saloon_address do |address| %>
<p>
<%= address.label :zip_code %><br>
<%= address.text_field :zip_code %>
</p>
<p>
<%= address.label :street %><br>
<%= address.text_field :street %>
</p>
<p>
<%= address.label :complement %><br>
<%= address.text_field :complement %>
</p>
<p>
<%= address.label :number %><br>
<%= address.text_field :number %>
</p>
<p>
<%= address.label :city %><br>
<%= address.text_field :city %>
</p>
<p>
<%= address.label :state %><br>
<%= address.text_field :state %>
</p>
<p>
<%= address.label :zip_code %><br>
<%= address.text_field :zip_code %>
</p>
<% end %>
<p>
<%= form.submit %>
</p>
<% end %>
and my controller:
class SaloonController < ApplicationController
def new
end
def show
@saloon = Saloon.find params[:id]
end
def index
@saloons = Saloon.all
end
def create
@saloon = Saloon.create(saloon_params)
redirect_to @saloon
end
private
def saloon_params
params.require(:saloon).permit(:name, :specialty, :online, :services, :average_price, :telephone,
saloon_address_attributes: [:saloon_id, :street, :complement, :number, :city, :state, :zip_code, :latitude, :longitude])
end
end
but when i try to save, i always get the message:
Unpermitted parameter: :saloon_addresss
any ideias of what is happening?
I've searched and tried so many different ways to permit, but i couldn't achieve it.
Update
I've done like the first answer:
class Saloon < ApplicationRecord
has_many :saloon_addresses
accepts_nested_attributes_for :saloon_addresses
end
But now i'm getting this error:
Started POST "/saloon" for 127.0.0.1 at 2017-12-13 00:21:25 -0200
Processing by SaloonController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"uF10p+ZHFiuard/CPxlt/yDgWhfzg2ttxdAWB81JTgLb+rC3ZTY+sNAgOWG3HRE8VKzHYP09QjkrL4ZMxQ3FFw==", "saloon"=>{"name"=>"asdjha", "specialty"=>"dsakhadksj", "services"=>"sdakha", "telephone"=>"21312312", "online"=>"1", "saloon_addresses"=>{"zip_code"=>"213213", "street"=>"sadasjdsa", "complement"=>"dsakhjdkasjh", "number"=>"23", "city"=>"sadjh", "state"=>"sadhj"}}, "commit"=>"Save Saloon"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
ActiveRecord::AssociationTypeMismatch (SaloonAddress(#70246611463840) expected, got ["street", "sadasjdsa"] which is an instance of Array(#70246530918320)):
app/controllers/saloon_controller.rb:14:in `create'