I am trying to create an ordering process in the online store. The user opens a basket (Cart object) in which products are visible (LineItem objects). At the same time, an Order object is immediately created in which the finished order will be written. This is what Client :: CartsController looks like:

def index if @cart.nil? || @cart.line_items.count == 0 empty_cart else @order = Order.new @cart = @cart.line_items.includes(:item) end end 

This is how the basket looks like:

 = simple_form_for [:client, @order], url: client_orders_path, method: :post, action: 'create' do |f| - if user_signed_in? = f.input :user_id, as: :hidden, input_html: { value: current_user.id } %table.table.table-hover.table-bordered#cartTable{ 'data-attr_id' => session[:cart_id] } Здесь в таблице описание заказов = f.input :address = f.input :phone_num, as: :tel - if user_signed_in? = f.button :submit, 'Оформить заказ', class: 'btn-primary' 

Next, by clicking the "Checkout" button, the form should go to the Client :: OrdersController controller, but I see the following error:

 No route matches [PUT] "/client/orders". 

Here is the routes.rb file

 Rails.application.routes.draw do scope 'admin' do devise_for :admins, :controllers => { :confirmations => 'admin/admins/confirmations', # :omniauth_callbacks => 'admin/admins/omniauth_callbacks', :passwords => 'admin/admins/passwords', :registrations => 'admin/admins/registrations', :sessions => 'admin/admins/sessions', :unlocks => 'admin/admins/unlocks', } end scope 'client' do devise_for :users, :controllers => { :confirmations => 'client/users/confirmations', # :omniauth_callbacks => 'admin/admins/omniauth_callbacks', :passwords => 'client/users/passwords', :registrations => 'client/users/registrations', :sessions => 'client/users/sessions', :unlocks => 'client/users/unlocks', } end namespace :admin do root 'items#index' resources :items end namespace :client do root 'items#index' resources :orders do resources :order_items end resources :carts, :items do resources :line_items end end root 'client/items#index' 

I do not understand why the PUT method is called in this case. Now it seems that in order to avoid an error, it lacks an id, but I need to call the create action. How do you need to set the form so that this error does not exist and the request goes to OrderControoler # create?

  • You seem to be indented in the markup. Check back. - D-side
  • one
    Show the generated html, namely the <form ..... > tag <form ..... > You pass the action, the url is all unnecessary, the simple_form_for helper recognizes by the object, create it or update. The same with url. - Pavel Volzhin
  • Yes, really incorrectly inserted haml markup here, corrected. Generated form: <form novalidate="novalidate" class="simple_form new_order" id="new_order" action="/client/orders" accept-charset="UTF-8" method="post"> Yes, I first tried without action and url, but the error is the same. Maybe the fact is that I generate the form in one controller (carts), and then I want it to be processed in another (orders)? - Mr. DannyX
  • This is a form to create , it seems to be submitted another. No, the form is not generated in the controller, but in the view, and its connection with the controller is minimal. - D-side

1 answer 1

Run the command

 rake routes 

See which paths are available for / client / orders. For a PUT, the path must be with an identifier: id

 /client/orders/:id 

Your path is not tied to an order with a specific id

 No route matches [PUT] "/client/orders".