Reputation: 349
I have a form that I wanna edit some informations, so I drew a get route for our edit form and a update route.
Routes.rb:
resource :guide_dog_forms
get '/cao-guia/:id/editar', to: 'guide_dog_forms#edit', as: :edit_guide_dog_form
put '/cao-guia/:id/', to: 'guide_dog_forms#update'
After that I edited my controllers, so i added the actions like these:
class GuideDogFormsController < ApplicationController
def create
@contact = GuideDogForm.new(params[:contact])
if @contact.save
GuideDogMailer.delay_for(10.seconds, retry: true).create(@contact)
render nothing: true, status: 200
else
render nothing: true, status: 400
end
end
def edit
@guidedoguser = GuideDogForm.find(params[:id])
end
def update
@guidedoguser = GuideDogForm.find(params[:id])
@guidedoguser.update(name: params[:name], email: params[:email])
redirect_to guide_dog_form_path(@guidedoguser)
end
end
My current rake routes is like this:
guide_dog_forms POST - /guide_dog_forms(.:format) - guide_dog_forms#create
new_guide_dog_forms GET - /guide_dog_forms/new(.:format) - guide_dog_forms#new
edit_guide_dog_forms GET - /guide_dog_forms/edit(.:format) - guide_dog_forms#edit
GET - /guide_dog_forms(.:format) - guide_dog_forms#show
PATCH - /guide_dog_forms(.:format) - guide_dog_forms#update
PUT - /guide_dog_forms(.:format) - guide_dog_forms#update
DELETE - /guide_dog_forms(.:format) - guide_dog_forms#destroy
When I submit my edit form, the url rendered is localhost:3000/guide_dog_forms.80
(80 is the ID of form that I trying edit) and not that which I defined on my routes. Why this is happens? Anyone can help me? Thanks.
Upvotes: 0
Views: 900
Reputation: 6952
That's a by-product of your resource
routes.
Try running rake routes
. You should see the guide_dog_form_path
route there with the output /guide_dog_form/:id
, which explains the redirect. You may have to set up another named route and redirect there if you wish to override the behavior of the resource
method.
Usually you would pass the singular version of the object to resource
. i.e. resource :guide_dog_form
or the plural to resources
. i.e. resources :guide_dog_forms
Based on the code you've provided, I imagine your intent is:
resources :guide_dog_forms
*FWIW, as of Rails 4 it's customary to use the PATCH
verb instead of PUT
to update records.
Upvotes: 1