I have two models
Client.rb has_many :addresses accepts_nested_atributes_for :addresses Address.rb has_many :addresses
I created a new action update_establishments on the ClientsController in order to add new establishments to a client.
ClientsController.rb
#the corresponding view of this action will render the locations_form
def add_establishments
@client = Client.find(params[:id])
@client.addresses.build
end
def update_establishments
create_zip
respond_to do |format|
if @client.update(client_params)
set_zip
format.html { redirect_to @client, notice:'Establishment added.'}
format.json {head :no_content}
else
format.html { render action: 'edit'}
format.json { render json: @client.error, status: :unprocessable_entity }
end
end
end
locations_form
%= form_for @client, :url=>{:action=>'update_establishments'}, html:{ :method =>"patch"} do |form| %>
I have two questions:
1 - When the form is rendered, the form is presenting two separate fields for the addresses, one containing the values of the existing address, and another to add a new. How can I make not to present the existing addresses?
2 - It gives an obvious error on the update_establishments action because the @client is nil. Is there a way to send the client itself from the form to the action or do I have to add hidden fields with the values?