0

How do I send an HTTP method in a route helper? i.e. I would like to call the link below from a controller action:

<%= link_to 'Submit to Scheduling', orders_path(cart_id: @cart), method: :post, data: { confirm: "Are you sure?" }, class: "primary button btn" %> 

I tried orders_path(cart_id: @cart) but that didn't work.

Neither does orders_path(cart_id: @cart, method: :post)

How do I do that?

Edit 1

orders_path GET /orders(.:format)     orders#index
            POST    /orders(.:format) orders#create
new_order_path  GET /orders/new(.:format) orders#new
edit_order_path GET /orders/:id/edit(.:format) orders#edit
order_path  GET /orders/:id(.:format) orders#show
            PATCH   /orders/:id(.:format) orders#update
            PUT /orders/:id(.:format) orders#update
            DELETE  /orders/:id(.:format) orders#destroy
marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • you need to provide your routes, on project direcotry print `rails routes` and you will get a lot of routes, just copy here orders few lines of routes. – Kamal Panhwar Jan 22 '20 at 05:37
  • It looks good, it is going straight to your controller do you have `app/controllers/orders_controller.rb` file with a method `def create` `puts params` `end` , as you have no error and it is going straight to controller create method. – Kamal Panhwar Jan 22 '20 at 05:49
  • Yeah, I do have a scaffolded `create` method within `orders_controller.rb`. – marcamillion Jan 22 '20 at 05:50
  • Looks like I could not understand your question you want to print this url within controller? than you can use `puts Rails.application.routes.url_helpers.orders_path(order_id: @order)` but it really not make any sense why you want to print it in controller, better place would be use model or view helper. – Kamal Panhwar Jan 22 '20 at 06:01
  • This is what I am trying to do -- https://stackoverflow.com/questions/59853763/how-do-i-redirect-after-sign-up-path-for-to-a-create-action-of-another-controlle – marcamillion Jan 22 '20 at 06:24

1 Answers1

0

Well unfortunately you can't send post method through redirect_to method. You must think about it by using get method. The easy way is to make another page where you take all your parameters in Get and that page can send post method using form_url. But still I won't consider it as it seem security issue sending parameters in get and then taking them to post method. But still it is your call so following is way to do using another page. Anotehr way is to add a gem repost your Gemfile

gem repost

Now use following method to send redirect

redirect_post(orders_path(order_id: @order))

It will send a post request automatically, but remember you have to fix token validation issue, and disable it in your controller with following line

skip_before_action :verify_authenticity_token

As for alert box which is purely view job, so if you want that too, then you have to create actually views and controller to redirect and show this popup alert message.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
  • This is too cumbersome for me. I discovered `url_for`, but that doesn't quite do it for me because it doesn't allow me to specify the HTTP verb and it isn't obeying the action parameter I specified, which you can see here - https://stackoverflow.com/questions/59853763/how-do-i-redirect-after-sign-up-path-for-to-a-create-action-of-another-controlle#59854295 – marcamillion Jan 22 '20 at 07:39
  • The above solution is working and perfect, if you want you can make your own form view and setup it like you want, but url_for does not have post method neither you can setup it like that. If you want you can but repost has also manage it using view and post method being called that way only. as it is only way to do it. Good luck. – Kamal Panhwar Jan 22 '20 at 07:44