简体   繁体   中英

form_tag No route matches [POST]

I've a custom method buy inside order controller

I've declared a special route with the following: which is sell_order_path

resources :orders do
  post 'buy', on: :member
end

And here's my form tag

<%= form_tag(buy_order_path(@symbol)) do %>
<%= hidden_field_tag :stock_price, '55' %>
<%= label_tag :stock_quantity, 'Buy Quantity' %>
<%= text_field_tag :stock_quantity, params[:stock_quantity] %> 
<%= submit_tag "Buy", class: "btn" %>
<% end %>

However, when I clicked submit, it says No route matches [POST] "/orders/CC3.SI/buy"

It work when I used

<%= form_tag(buy_order_path('5')) do %> 

However, I would need a dynamic. Any idea how? I have a feeling that it how I created the route.

By the way @symbol is just a variable right now, not an object

Here's what I did

Instead of

resource :order do
  post :buy, on: :member
end

which will generate a route of

/orders/:id/buy(.:format)

I changed my route to

resource :order do
  post :buy, on: :collection
end

which will generate a route of

/orders/buy(.:format)

Following on, I insert the symbol parameter into my form_tag

<%= form_tag(buy_order_path()) do %>
<%= hidden_field_tag :symbol, @symbol %>
<%= hidden_field_tag :stock_price, '55' %>
<%= label_tag :stock_quantity, 'Buy Quantity' %>
<%= text_field_tag :stock_quantity, params[:stock_quantity] %> 
<%= submit_tag "Buy", class: "btn" %>
<% end %>

The above work , however I am not sure if this is the best way as user might be able to see the hidden field tag and edit the html value. Is there a better way for to pass value to controller?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM