Question:
I'm trying to create an online store checkout process. The user opens the cart (Cart object), in which the products (LineItem objects) are visible. Together with this, an Order object is immediately created, into 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 itself looks:
= 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'
Further, by clicking the "Checkout" button, the form should go to the Client :: OrdersController, 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'
It is not clear to me why the PUT method is being called in this case. Now, apparently, in order to avoid an error, it lacks an id, but I need to call the create action. How do you set the form so that there is no such error and the request goes to OrderControoler # create?
Answer:
Run the command
rake routes
See what paths are available for / client / orders For 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".