我尝试不对此提出任何问题,自己弄清楚,但现在我茫然,认为我可能必须重新开始:(。我用devise
生成了Rails mysql项目,类别的脚手架,然后是类似的产品:
rails g scaffold category name:string description:text
rails g scaffold product name:string category:references
我的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
我的products_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
和我的耙路:
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
很抱歉,这篇文章太长了,我不想遗漏任何内容。无论如何,实际上只需要routes.rb,因为用户被路由到类别中的“显示”产品,而不是“删除”,所以我试图从类别中“删除”产品。并且product / index.html.erb具有一个“删除”链接,如下所示,但是仅显示了一个产品,
<td><%= link_to 'Delete', action: "destroy", id: product.id, method: :delete, data: { confirm: 'Are you sure?' } %></td>
答案 0 :(得分:0)
我建议您的route.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
这将为您提供以下路线:
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
请注意,这为您提供了new_category_product
和category_products
,因此您不需要:
post '/categories/:category_id/products/new' => 'products#new'
put '/categories/:categoriy_id/products/:id' => 'products#create'
(顺便说一句,:categoriy_id
在第二行中拼写错误。)
还要注意浅层嵌套,您可以在the Guide, section 2.7 Nested Resources中了解更多信息。
然后,您应该可以:
<td>
<%= link_to 'Delete', product, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>