rails 4 link_to post method

I have a rails 4 app that has a link_to with a post method but I keep getting:

No route matches [GET] “/payments/136330”

<%= link_to(“Buy Now”, buy_path(list_item), :method => :post) %>

Ideas??

Route, post ‘payments/:list_item_id’, to: ‘payments#buy’, :as => :buy

Please change your name from “Me” to something else. It’s confusing.

Hi,

Can you copy your full routes.rb, I believe you should place the route at before the route
“resources :payment”

Regards,
Logan

That is the only route for that controller.

Post your routes.rb file

a little difficult without a full log, but a strong, educated guess would be that there’s a javascript problem. Links always generate a GET action. When you use :method => :post, there’s javascript in the rails javascript file that detects it, re-writes the link as a form, and submits it as a post. If that javascript is missing, or if there’s a javascript error that prevents it from executing, you’ll get the message you’re getting above.

@mike2r

Thanks, here is the link generated, Buy Now

Routes:

resources :users

resources :list_items do

post :buy, :on => :member

end

root ‘list_items#index’

ERB:

<%= link_to(“Buy Now”, buy_list_item_path(list_item), :method => ‘post’) %>

Thanks, here is the link generated, <a data-method="post" href="/list_items/136298/buy" rel="nofollow">Buy Now</a>

Routes:

resources :users
  resources :list_items do
    post :buy, :on => :member
  end

  root 'list_items#index'

ERB:

<%= link_to("Buy Now", buy_list_item_path(list_item), :method => 'post') %>

Confirm that rails.js is being loaded. Without it, the unobtrusive stuff doesn't work.

Walter

jquery and jquery_ujs are loaded. it is a rails 4 app.

That’s not the same url as the once mentioned in the error. Is the post request completing OK and then redirecting to a non-existent url?

Fred

The one I posted was a slightly different one but still gives me the same error. It does not seem to recognize the link is a 'POST" even though the actual link on the page is a post.

No route matches [GET] “/list_items/136327/buy”

The action ‘buy’ is in the liat_items_controller.

The one I posted was a slightly different one but still gives me the same error. It does not seem to recognize the link is a 'POST" even though the actual link on the page is a post.

You’ll send people round in circles if you’re mixing up different urls & different errors. As others have said, this sounds suspicious like a javascript error. Check your browser console for javascript errors. You might also try sticking some (javascript) breakpoints in the rails ubobtrusive js code, to check what it is doing.

Fred