imtk
imtk

Reputation: 1548

No route matches [POST] "/services/new"

I'm using form object with reform gem to build a service form.

slim form:

= simple_form_for [:admin, @service_form], url: services_path

routes:

    services_path        GET    /services(.:format)             services#index
                         POST   /services(.:format)             services#create
    new_service_path     GET    /services/novo(.:format)        services#new
    edit_service_path    GET    /services/:id/editar(.:format)  services#edit
    service_path         GET    /services/:id(.:format)         services#show
                         PATCH  /services/:id(.:format)         services#update
                         PUT    /services/:id(.:format)         services#update

controller - new action: (ServiceFormObject is the form object class for the service)

def new
    service = Service.new        
    @service_form = ServiceFormObject.new(service)
end

With all that, I'm getting a No route matches [POST] "/services/new".

I don't know why it's searching for a POST in "/services/new" instead of "/services"

Any solution?

-- Rails 4, Reform, Simple_form, slim --

UPDATE

I'm using this same form to edit/update, so the hardcoded URL path is not the ideal solution.

I created this method:

 def is_persisted?
     if self.persisted?
       "service/#{@service_form.id}"
     else
       '/services'
     end 
 end

And I can use this on url, like this:

url: @service_form.is_persisted?

Upvotes: 1

Views: 412

Answers (1)

eabraham
eabraham

Reputation: 4164

If you change the form to this syntax, does it work?

= simple_form_for [:admin, @service_form], url:"/services"

EDIT @Richard Seviora is correct, a hardcoded path is not ideal. Try adding "as" to the POST route.

post 'services' => 'services#create', as: "create_service"

When you run rake routes does the POST have a path?

Upvotes: 1

Related Questions