[英]Rails 5.2 nested routes, and urls paths not working for delete
I have tried not to ask any questions regarding this and figure it out myself but now I am at a loss, thinking I might have to start over :(. I generated a rails mysql project with devise
, used scaffolding for categories, then products like so: 我试着不问任何问题,自己弄清楚,但现在我很茫然,认为我可能必须重新开始:(。我生成了一个带devise
的rails mysql项目,使用了脚手架来分类,然后使用诸如所以:
rails g scaffold category name:string description:text
rails g scaffold product name:string category:references
my routes.rb (for some reason create was not included with product/new) 我的routes.rb(由于某些原因create不包含在product / new中)
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
authenticated :user do
root 'categories#index', as: :authenticated_root
resources :categories do
resources :products
end
post '/categories/:category_id/products/new' => 'products#new'
put '/categories/:categoriy_id/products/:id' => 'products#create'
end
unauthenticated :user do
root 'devise/sessions#new', as: :unauthenticated_root
get 'sign-in' => 'devise/sessions#new'
post 'login' => 'devise/sessions#create'
delete 'logout' => 'devise/sessions#destroy'
end
end
end
my products_controller.rb 我的产品_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def destroy
@product.destroy
redirect_to category_products_path
end
private
def set_product
@product = Product.find(params[:id])
end
end
and my rake routes: 和我的耙路:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session GET|DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
authenticated_root GET / categories#index
category_products GET /categories/:category_id/products(.:format) products#index
POST /categories/:category_id/products(.:format) products#create
new_category_product GET /categories/:category_id/products/new(.:format) products#new
edit_category_product GET /categories/:category_id/products/:id/edit(.:format) products#edit
category_product GET /categories/:category_id/products/:id(.:format) products#show
PATCH /categories/:category_id/products/:id(.:format) products#update
PUT /categories/:category_id/products/:id(.:format) products#update
DELETE /categories/:category_id/products/:id(.:format) products#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
POST /categories/:category_id/products/new(.:format) products#new
PUT /categories/:categoriy_id/products/:id(.:format) products#create
unauthenticated_root GET / devise/sessions#new
sign_in GET /sign-in(.:format) devise/sessions#new
login POST /login(.:format) devise/sessions#create
logout DELETE /logout(.:format) devise/sessions#destroy
Sorry for the length of this post I didn't want to leave anything out. 很抱歉,这篇文章太长了,我不想遗漏任何内容。 Really only needed routes.rb, anyway, as is the user is routed to 'show' product from categories, rather than 'delete', I am trying to 'delete' a product from categories. 无论如何,实际上只需要routes.rb,因为用户被路由到类别中的“显示”产品,而不是“删除”,所以我试图从类别中“删除”产品。 And the product/index.html.erb has a 'delete' link as shown below, however that just shows a product, 而且product / index.html.erb具有一个“删除”链接,如下所示,但是仅显示了一个产品,
<td><%= link_to 'Delete', action: "destroy", id: product.id, method: :delete, data: { confirm: 'Are you sure?' } %></td>
I would suggest that your routes.rb look more like: 我建议您的routes.rb看起来更像:
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
authenticated :user do
root 'categories#index', as: :authenticated_root
resources :categories, shallow: true do
resources :products
end
end
unauthenticated :user do
root 'devise/sessions#new', as: :unauthenticated_root
get 'sign-in' => 'devise/sessions#new'
post 'login' => 'devise/sessions#create'
delete 'logout' => 'devise/sessions#destroy'
end
end
end
This will give you routes like: 这将为您提供以下路线:
category_products GET /categories/:category_id/products(.:format) products#index
POST /categories/:category_id/products(.:format) products#create
new_category_product GET /categories/:category_id/products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
Note that this gives you new_category_product
and category_products
, so you don't need: 请注意,这为您提供了new_category_product
和category_products
,因此您不需要:
post '/categories/:category_id/products/new' => 'products#new'
put '/categories/:categoriy_id/products/:id' => 'products#create'
(BTW, :categoriy_id
is misspelled in the second line.) (顺便说一句, :categoriy_id
在第二行中拼写错误。)
Also note shallow nesting, which you can read more about in the Guide, section 2.7 Nested Resources . 还要注意浅层嵌套,您可以在《指南》的2.7嵌套资源一节中了解更多信息。
Then, you should be able to do: 然后,您应该能够:
<td>
<%= link_to 'Delete', product, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.