Reputation: 511
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?
Upvotes: 0
Views: 278
Reputation: 1240
Question 1, when you call @client.addresses.build
it is building a third address for the client. My guess is that you are then showing all three addresses on the form.
Question 2, you'll want to send the client id with the request, and use that to find the client.
The following should solve both problems.
As much as possible - and it is almost always possible - try to stick with the standard Rails controller actions. Nonstandard controller action like add_establisments
or update_establishments
are usually a sign that there is a more "Rails" way to model your controllers.
In this case, I would question why ClientsContoller
is handling establishments. If you add an EstablishmentsController
, my guess is that things will just start to work. Here is how to do that:
In config/routes.rb, create a clients resource that has a nested establishments resource.
resources :clients do
resources :establishments
end
This will give you some nested routes which will all start with /clients/:client_id/establishments.
Create an EstablishmentsController
and move add_establishments
and update_establishments
to it. Rename add_establishments
to new
and rename update_establishments
to update
. Also move the corresponding views to /views/establishments/ and rename them to match their new action names.
If there's no compelling reason to find the client in the new
action, you can just build a new establishment to pass to the view. Either way, your form will be associated with this new establishment, not with the existing client. (Question 1)
In update
, you'll use the :client_id
param to find the client. (Question 2) update
will need a bit of massaging, but I'd need to know if you're really updating multiple establishments at once or just a single establishment to say what it would look like. (You won't be updating @client
itself, but the client's establishments.)
Upvotes: 1