Alb
Alb

Reputation: 221

No route matches POST confusion

I am very unfamiliar with routing and the entire back-end of Rails in general. I am attempting to have a click on "edit cart" lead to the edit page, I have the edit_cart_path and corresponding view- but when I click the edit cart button, I get

Routing Error
No route matches [POST] "/carts/21/edit"

I have resources :carts in routes.rb, I have get "/carts/:id/edit" => "carts#edit" as well. Have tried a couple other methods including "via: get". Why is it insisting on POST, and how to solve this?

Upvotes: 0

Views: 105

Answers (1)

theodorton
theodorton

Reputation: 674

I'm guessing you're doing something like this, in your view:

button_to(edit_cart_path(@cart))

When using a button_to helper, the default HTTP method will be POST.

You'll have to do explicitly define the HTTP method you want to execute:

button_to(edit_cart_path(@cart), method: :get)

I would encourage you to use the link_to helper instead, and add any button effect using CSS:

link_to(edit_cart_path(@cart), class: 'btn')

From the Rails 4 documentation:

button_to(name, options = {}, html_options = {})

The options hash accepts the same options as url_for.

There are a few special html_options:

:method - Symbol of HTTP verb. Supported verbs are :post, :get, :delete and :put. By default it will be :post.

Upvotes: 4

Related Questions